转自:
http://hi.baidu.com/eternized/blog/item/75dc2f8fdb0576f2503d92b6.html/cmtid/f4d6f536c57ef9d3a2cc2ba4
测试时间 1、很不精确的time(), 精确到秒 #include <time.h> ... int start = time(NULL); ... int during = time(NULL) - start; 2、更精确一点的clock(),精确到1/16秒 #include <time.h> ... double start = clock(); ... double during = double(clock() - start) / CLOCKS_PER_SEC; 3、根据CPU时间来测的时间,只适用于windows,精确到微秒 #include <windows.h> ... LARGE_INTEGER t1,t2,feq; QueryPerformanceFrequency(&feq); QueryPerformanceCounter(&t1); ... QueryPerformanceCounter(&t2); double during = double(t2.QuadPart-t1.QuadPart)/feq.QuadPart; //P.S. 这个LARGE_INTEGER其实就是一个封装了long long在里面的 结构体。 4、根据实际使用的CPU时间(用户时间)来测的时间,只适用于win2000以后系统,精确到100毫秒。 #include <windows.h> ... LARGE_INTEGER feq; QueryPerformanceFrequency(&feq); FILETIME l1, l2, l3, l4; GetProcessTimes(GetCurrentProcess(), &l1, &l2, &l3, &l4); ... ULARGE_INTEGER utime; utime.LowPart = l4.dwLowDateTime; utime.HighPart = l4.dwHighDateTime; double during = (double)utime.QuadPart / feq.QuadPart; //P.S. 这个可以用于同时跑多进程的online judge系统,虽然不够精确。 |