QueryPerformanceCounter()函数返回高精确度性能计数器的值,它可以以微妙为单位计时.
QueryPerformanceCounter()确切的精确计时的最小单位是与系统有关的,QueryPerformanceFrequency()提供了这个系统频率值,它返回每秒嘀哒声的个数.
int gettimeofday(struct timeval *tv, void *tzp)
{
static struct timeval stv = { 0 }; /* initial timeval */
static INT64 sct = 0, tick = 0; /* Query tick */
static int initialized = -1; /* initial flag */
LARGE_INTEGER startCount = { 0 }; /* Start Count */
LARGE_INTEGER tickPerSec = { 0 }; /* Tick per second */
struct timeb tb = { 0 }; /* Timeb struct */
LARGE_INTEGER count = { 0 };
INT64 c = 0;
if (initialized == -1) {
ftime(&tb);
stv.tv_sec = (long)tb.time;
stv.tv_usec = tb.millitm * 1000;
if (!QueryPerformanceFrequency(&tickPerSec)) {
LOG_MSG("QueryPerformanceFrequency(): No high-reso counter.\n");
return -1;
}
if (!QueryPerformanceCounter(&startCount)) {
LOG_MSG("QueryPerformanceCounter(): No high-reso counter.\n");
return -1;
}
sct = startCount.QuadPart; tick = tickPerSec.QuadPart;
initialized = 0;
}
if (!QueryPerformanceCounter(&count))
return -1;
c = count.QuadPart;
tv->tv_sec = stv.tv_sec + (long)((c - sct) / tick);
tv->tv_usec = stv.tv_usec + (long)(((c - sct) % tick) * 1000000 / tick);
if (tv->tv_usec >= 1000000) {
tv->tv_sec++;
tv->tv_usec -= 1000000;
}
return 0;
}
http://www.cnblogs.com/dwdxdy/p/3214905.html