• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • Last Modified:

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.
0
MoonCalf
Asked:
MoonCalf
  • 3
  • 2
1 Solution
 
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
 
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now