Calculate small programs for CPU speed

zhaozj2021-02-16  54

The following procedure compiles running in the VC60, Console mode. You can try it, it is very accurate.

#include

#include

Float measure_clock_speed ();

Int main (void)

{

Printf ("THE CPU IS Running In% F MHz", Measure_Clock_Speed ​​());

GetChar ();

Return 1;

}

Float measure_clock_speed ()

// -------------------------------------

{

Unsigned long Ticks;

Unsigned long cycles;

Unsigned long stamp0,

Stamp1;

Unsigned long freq = 0;

Unsigned long freq2 = 0;

Unsigned long freq3 = 0;

Unsigned long Total;

Unsigned long Tries = 0;

Large_integer t0, t1;

Large_integer count_freq;

if (! queryperformancefrequency (& count_freq))

{

Return 0.0F;

}

Unsigned long priority_class = getpriorityclass (getcurrentprocess ());

Long thread_priority = GetthreadPriority (getCurrentThread ());

SetPriorityClass (GetCurrentProcess (), realtime_priority_class;

SetthreadPriority (getCurrentThread (), thread_priority_time_critical;

DO

{

Tries ;

FREQ3 = FREQ2;

FREQ2 = FREQ;

QueryperFormanceCounter (& T0);

T1.LOWPART = T0.LOWPART;

T1.highpart = t0.highpart;

While (unsigned long) T1.LOWPART - (unsigned long) T0.LowPart <50)

{

QueryperFormanceCounter (& T1);

}

_asm

{

RDTSC

Mov Stamp0, EAX

}

T0.LowPart = T1.LowPart; // Reset Initial

T0.Highpart = T1.Highpart; // Time

While (unsigned long) t1.lowpart- (unsigned long) T0.LowPart <1000)

{

QueryperFormanceCounter (& T1);

}

_asm

{

RDTSC

Mov Stamp1, EAX

}

Cycles = stamp1 - stamp0;

Ticks = (unsigned long) t1.lowpart - (unsigned long) t0.lowpart;

Ticks = Ticks * 100000;

Ticks = ticks / (count_freq.lowpart / 10); if (ticks% count_freq.lowpart> count_freq.lowpart / 2)

{

Ticks ; // Round Up if Necessary

}

FREQ = cycles / ticks; // cycles / us = MHz

IF (Cycles% Ticks> Ticks / 2)

{

Freq ; // Round Up if Necessary

}

Total = (FREQ FREQ2 FREQ3);

WHILE ((Tries <3) || (Tries <20) && ((ABS (3 * Freq-Total)> 3)

||

(ABS (3 * Freq2-Total)> 3) || (ABS (3 * Freq3-Total)> 3)))))

IF (Total / 3! = (Total 1) / 3)

{

Total ; // Round Up if Necessary

}

// restore the thread priority

SetPriorityClass (), priority_class;

SetthreadPriority (GetCurrentThread (), Thread_Priority;

Return float / 3.0f;

}

转载请注明原文地址:https://www.9cbs.com/read-23897.html

New Post(0)