如何获取timestamp类型的精度到纳秒级
1个回答
展开全部
获取时间章,开始,停,测CPU速度 的 函数们如下。(MS VC++ 6.0 编译器。)
#define NOMINMAX
#include <windows.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
// "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; }
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);
}
void wait ( int seconds )
{
clock_t endwait;
endwait = clock () + seconds * CLOCKS_PER_SEC ;
while (clock() < endwait) {}
}
#define NOMINMAX
#include <windows.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
// "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; }
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);
}
void wait ( int seconds )
{
clock_t endwait;
endwait = clock () + seconds * CLOCKS_PER_SEC ;
while (clock() < endwait) {}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询