ACE中有一个类,ACE_High_Res_Timer,这里的Res即为Resolution,用OS相关的方法来获取精确的定时器。
其中静态方法global_scale_factor的原型为
ACE_UINT32 global_scale_factor()
打开对应的.cpp,可以看到这个函数在Linux平台下调用的是
ACE_High_Res_Timer::get_cpuinfo()
而get_cpuinfo获取的是当前的cpu 的Mhz数目。
在Ubunutu下
cat /proc/cpuinfo即可看到
cat /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 42
model name : Intel(R) Core(TM) i5-2415M CPU @ 2.30GHz
stepping : 7
cpu MHz :
2313.671
cache size : 6144 KB
fdiv_bug : no
hlt_bug : no
f00f_bug : no
coma_bug : no
fpu : yes
fpu_exception : yes
cpuid level : 5
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 syscall nx rdtscp lm constant_tsc up pni monitor ssse3 lahf_lm
bogomips : 4627.34
clflush size : 64
cache_alignment : 64
address sizes : 36 bits physical, 48 bits virtual
power management:
#include <ace/Memory_Pool.h>
#include <ace/High_Res_Timer.h>
#include <ace/Log_Msg.h>
int main()
{
//compare the get_cpuinfo and global_scale_factor
ACE_UINT32 t1 = ACE_High_Res_Timer::get_cpuinfo();
ACE_UINT32 t2 = ACE_High_Res_Timer::global_scale_factor();
ACE_DEBUG((LM_DEBUG,"%d %d\n",t1,t2)); //打印的值都是2314,相等
ACE_High_Res_Timer timer;
timer.start();
ACE_OS::sleep(5);
timer.stop();
ACE_TCHAR message[128] = {0};
timer.print_total(message);
fprintf(stderr,"%s",message);
return 0;
}
这里打印出的值都是2314 ,对应的是cpu的MHz数
ACE_High_Res_Timer有一个方法为print_total,能以更友好的方式打印出时间使用情况