CPU Speed Function.

Hi.  I need to work out the speed of the CPU, programmatically.  I don't want suggestions of components or anything like that....I need a function that I can put in a unit of my own.

Can you help me?

MoonCalf.
LVL 1
MoonCalfAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ranmanorCommented:
Here is a delphi program that does it.
Notice the FreqInfo record and the GetCpuSpeed function.

unit speed;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  Freq_info = Record
    Raw_Freq: Cardinal;       // Raw frequency of CPU in MHz.
    Norm_Freq: Cardinal;      // Normalized frequency of CPU in MHz.
    In_Cycles: Cardinal;      // Internal clock cycles during test
    Ex_Ticks: Cardinal;       // Microseconds elapsed during test
  end;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    function GetCpuSpeed: Freq_Info;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

{ TForm1 }

function TForm1.GetCpuSpeed: Freq_Info;
var
  Cpu_Speed: Freq_Info;
  t0, t1: Int64;
  freq, freq2, freq3, Total: Cardinal;
  Total_Cycles, Cycles: Cardinal;
  Stamp0, Stamp1: Cardinal;
  Total_Ticks, Ticks: Cardinal;
  Count_Freq: Int64;
  Tries, IPriority, hThread: Integer;
begin
  freq  := 0;
  freq2 := 0;
  freq3 := 0;
  tries := 0;
  total_cycles := 0;
  total_ticks := 0;
  Total := 0;
  hThread := GetCurrentThread();
  if (not QueryPerformanceFrequency(count_freq)) then
    begin
      Result := cpu_speed;
    end
  else
    begin
      while ((tries < 3 ) or ((tries < 20) and ((abs(3 * freq - total) > 3) or
             (abs(3 * freq2-total) > 3) or (abs(3 * freq3-total) > 3)))) do
      begin
        inc(tries);
        freq3 := freq2;
        freq2 := freq;
        QueryPerformanceCounter(t0);
        t1 := t0;
        iPriority := GetThreadPriority(hThread);
        if ( iPriority <> THREAD_PRIORITY_ERROR_RETURN ) then
          begin
             SetThreadPriority(hThread, THREAD_PRIORITY_TIME_CRITICAL);
          end;
        while (( t1 - t0) < 50) do
        begin
           QueryPerformanceCounter(t1);
           asm
              push eax
              push edx
              db   0Fh        // Read Time
              db   31h        // Stamp Counter
              MOV stamp0, EAX
              pop  edx
              pop  eax
           end;
        end;
        t0 := t1;
        while ((t1 - t0) < 1000) do
        begin
          QueryPerformanceCounter(t1);
          asm
            push eax
            push edx
            db   0Fh // Read Time
            db   31h // Stamp Counter
            MOV stamp1, EAX
            pop  edx
            pop  eax
          end;
        end;
        if ( iPriority <> THREAD_PRIORITY_ERROR_RETURN ) then
        begin
          SetThreadPriority(hThread, iPriority);
        end;
        cycles := stamp1 - stamp0;
        ticks :=  t1 - t0;
        ticks := ticks * 100000;
        ticks := Round(Ticks / (count_freq/10));
        total_ticks := Total_Ticks + ticks;
        total_cycles := Total_Cycles + cycles;
        freq := Round(cycles / ticks);
        total := (freq + freq2 + freq3);
      end;
      freq3 := Round((total_cycles * 10) / total_ticks);
      freq2 := Round((total_cycles * 100) / total_ticks);
      if (freq2 - (freq3 * 10) >= 6) then
        inc(freq3);
      cpu_speed.raw_freq := Round(total_cycles / total_ticks);
      cpu_speed.norm_freq := cpu_speed.raw_freq;
      freq := cpu_speed.raw_freq * 10;
      if((freq3 - freq) >= 6) then
        inc(cpu_speed.norm_freq);
      cpu_speed.ex_ticks := total_ticks;
      cpu_speed.in_cycles := total_cycles;
      Result := cpu_speed;
    end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
  Freq: Freq_Info;

begin
  Freq:= GetCpuSpeed;
  ShowMessage(IntTosTR(Freq.Raw_Freq));
end;

end.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LischkeCommented:
I wonder whether this code works with WinNT too?

Ciao, Mike
0
MoonCalfAuthor Commented:
Cheers big ears.....

By the way.....

If X=Y Then
Begin
  ShowMessage('X is equal to Y');
End;

is the same as...

If X=Y Then
  ShowMessage('X is equal to Y');

Know what I mean  : )

Ta.
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

MoonCalfAuthor Commented:
Cheers big ears.....

By the way.....

If X=Y Then
Begin
  ShowMessage('X is equal to Y');
End;

is the same as...

If X=Y Then
  ShowMessage('X is equal to Y');

Know what I mean  : )

Ta.
0
MoonCalfAuthor Commented:
Mike....I use NT, and it does work.
0
LischkeCommented:
Fine
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.