上一篇:测试2个类型相同的方法 >>
计算cpu速度的小程序
#include <stdio.h>
#include <windows.h>
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(GetCurrentProcess(), priority_class);
SetThreadPriority(GetCurrentThread(), thread_priority);
return float(total) / 3.0f;
}
下一篇:Union的迷思 >>
相关文章:
- · 数据结构学习(C++)——栈应用(表达式求值)
- · 解除心头的困惑--纯虚函数
- · 快捷方式的函数
- · WTL体系结构(2)
- · 恶草丛生的阴暗角落---虚拟机制(下)
- · 深度解析VC中的消息(下)
- · 数据结构学习(C++)——队列应用(事件驱动模拟)
- · 新手对COM的认识及疑惑
- · 全面解读WM_NOTIFY
- · 开发集成Microsoft Visual Basic for Application的应用系统(一)
- · 浅谈 MFC 的子类化机制和该机制的一个应用(1)
- · 深度剖析消息反射机制
- · 浅谈 MFC 的子类化机制和该机制的一个应用(2)
- · 我的CLog的实现
- · Hello World by Microsoft Speech SDK 5.1
- · 初级X编程2
- · 彻底粉碎“指针与数组的困惑”(上)
- · ADSL组建局域网快捷方案
- · ADSL组建局域网快捷方案
- · 形形色色的自定义消息(上)
- · 数据结构学习(C++)——线性链式结构总结(代后记)【1】
- · vc入门宝典(九)
- · 形形色色的自定义消息(下)
- · C++中的预处理(上)
- · 非常时期突现网络办公的魅力!
- · asp_dll
- · 学Java,观GP
- · 怎样写一个 NT 服务程序
- · 代码优化试验——短循环优化(上)
- · 代码优化试验——短循环优化(下)
- · =========<C++未眠夜---我学习C++的心路历程>========
- · BOOK
- · 读《Efficient C++》疑惑
- · C++中的预处理(下)
- · DSP v1.0--序列化和反序列化对象和DNS v1.0--得到域的邮件服务器
- · C++入门解惑(0)——序
- · 代码自动完成、文档自动生成、提高开发效率----介绍VcExtend
- · “瑜珈山夜话”---序
