硬件内部计时器精度 US级别
#include<iostream> #include "functional" #include "windows.h" using namespace std; #include <WinBase.h> long long calculateMS(std::function<void()> processFunc) { long long _value; LARGE_INTEGER freq, _start, _end; QueryPerformanceFrequency(&freq); QueryPerformanceCounter(&_start); processFunc(); QueryPerformanceCounter(&_end); _value = (_end.QuadPart - _start.QuadPart) * 1000 / freq.QuadPart; return _value; } long long calculateUS(std::function<void()> processFunc) { long long _value; LARGE_INTEGER freq, _start, _end; QueryPerformanceFrequency(&freq); QueryPerformanceCounter(&_start); processFunc(); QueryPerformanceCounter(&_end); _value = (_end.QuadPart - _start.QuadPart) *1000*1000 / freq.QuadPart; return _value; } int main(int argc, char *argv[]) { printf("%d us\r\n\n", calculateUS([=]() { int a[9999]; for (int i = 0; i != 9999; i++) { a[i] = i; } })); system("pause"); return 0; }
时间: 2024-10-01 02:35:11