C语言获取CPU tick
发布网友
发布时间:2022-04-20 05:45
我来回答
共3个回答
热心网友
时间:2023-09-11 08:55
如果是获取 cpu 时钟 的 tick:
clock_t tick1,tick2;
tick1=clock(); // 开机到执行这句时的毫秒数 ms
等待一会
tick2=clock(); // 开机到执行这句时的毫秒数 ms
dt = (double) (tick2 - tick1); // 或得时间差。
===============
如果是 获取 CPU cycle count
#include <stdint.h>
// Windows
#ifdef _WIN32
#include <intrin.h>
uint64_t rdtsc(){
return __rdtsc();
}
// Linux/GCC
#else
uint64_t rdtsc(){
unsigned int lo,hi;
__asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
return ((uint64_t)hi << 32) | lo;
}
#endif
===================
获取高精度时间(MS VC++ 6.0编译器):
// Pentium instruction "Read Time Stamp Counter".
__forceinline unsigned _int64 My_clock(void)
{
_asm _emit 0x0F
_asm _emit 0x31
}
unsigned _int64 Start(void) { return My_clock();}
unsigned _int64 Stop(unsigned _int64 m_start, unsigned _int64 m_overhead)
{return My_clock()-m_start - m_overhead; }
==========
获取cpu 速度(MS VC++ 6.0编译器):
void get_CPU_speed()
{
unsigned _int64 m_start=0, m_overhead=0;
unsigned int CPUSpeedMHz;
m_start = My_clock();
m_overhead = My_clock() - m_start - m_overhead;
printf("overhead for calling My_clock=%I64d\n", m_overhead);
m_start = My_clock();
wait_ms(2000);
CPUSpeedMHz=(unsigned int) ( (My_clock()- m_start - m_overhead) / 2000000);
printf("CPU_Speed_MHz: %u\n",CPUSpeedMHz);
}
热心网友
时间:2023-09-11 08:55
getTickCount()
热心网友
时间:2023-09-11 08:56
好时尚是重实践。