📄 timing.cpp
字号:
// timing.cpp#include "timing.hpp"#include <iostream>#include <stdlib.h>static unsigned int timerRes;#ifdef WIN32#include <windows.h>/* * GetTime -- * * Returns the curent time (from some uninteresting origin) in usecs * based on the performance counters. */int64GetTime(void){ static double cycles_per_usec; LARGE_INTEGER counter; if (cycles_per_usec == 0) { static LARGE_INTEGER lFreq; if (!QueryPerformanceFrequency(&lFreq)) { std::cerr << "Unable to read the performance counter frquency!\n"; return 0; } cycles_per_usec = 1000000 / ((double) lFreq.QuadPart); } if (!QueryPerformanceCounter(&counter)) { std::cerr << "Unable to read the performance counter!\n"; return 0; } return ((int64) (((double) counter.QuadPart) * cycles_per_usec));}// Tim is evil...#pragma comment(lib,"winmm")unsigned int GetTimeMillis(void) { return (unsigned int)timeGetTime();}// by default in 2000/XP, the timeGetTime call is set to some resolution between 10-15 ms// query for the range of value periods and then set timer to the lowest possible. Note:// MUST make call to corresponding CleanupMillisTimervoid SetupMillisTimer(void) { TIMECAPS timeCaps; timeGetDevCaps(&timeCaps, sizeof(TIMECAPS)); if (timeBeginPeriod(timeCaps.wPeriodMin) == TIMERR_NOCANDO) { std::cerr << "WARNING: Cannot set timer precision. Not sure what precision we're getting!\n"; } else { timerRes = timeCaps.wPeriodMin; }}void CleanupMillisTimer(void) { if (timeEndPeriod(timerRes) == TIMERR_NOCANDO) { std::cerr << "WARNING: bad return value of call to timeEndPeriod.\n"; }}#else#include <unistd.h>#include <sys/time.h>void SetupMillisTimer(void) {}void CleanupMillisTimer(void) {}int64 GetTime (void) { struct timeval tv; timerRes = 1000; gettimeofday(&tv,NULL); int64 temp = tv.tv_usec; temp+=tv.tv_sec*1000000; return temp;}unsigned int GetTimeMillis () { return (unsigned int)(GetTime ()/1000);}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -