用C措施的执行时间(在Windows上)(Measure execution time in C (

2019-08-18 08:03发布

有没有更好的函数或方法来衡量的时间比clock()在Windows功能? 我有一个简短的操作,当我尝试clock()gettickcount()它说,它采取0.0秒。 我需要一种方法通过毫秒或纳秒来衡量它。

Answer 1:

您可以使用QueryPerformanceCounterQueryPerformanceFrequency

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

int main(void)
{
    LARGE_INTEGER frequency;
    LARGE_INTEGER start;
    LARGE_INTEGER end;
    double interval;

    QueryPerformanceFrequency(&frequency);
    QueryPerformanceCounter(&start);

    // code to be measured

    QueryPerformanceCounter(&end);
    interval = (double) (end.QuadPart - start.QuadPart) / frequency.QuadPart;

    printf("%f\n", interval);

    return 0;
}


Answer 2:

您可以使用QueryPerformanceCounter的联合QueryPerformanceFrequency的获得纳秒的精度。



文章来源: Measure execution time in C (on Windows)