简体   繁体   English

用C测量时间

[英]Measuring time in C

I'm trying to measure some activity in C (Matrix multiplying) and noticed that I should do something like this: 我正在尝试用C(矩阵乘法)测量一些活动,并注意到我应该做这样的事情:

clock_t start = clock();
sleep(3);
clock_t end = clock();
double elapsed_time = (end - start)/(double)CLOCKS_PER_SEC;
printf("Elapsed time: %.2f.\n", elapsed_time);

The output is: 输出是:

Elapsed time: 0.00.

Why is this happening? 为什么会这样?

clock estimates the CPU time used by your program; clock估计程序使用的CPU时间; that's the time the CPU has been busy executing instructions belonging to your program. 这是CPU忙于执行属于您的程序的指令的时间。 sleep doesn't perform any work, so it takes no noticeable CPU time (even if it takes wallclock time). sleep不执行任何工作,因此它不会占用明显的CPU时间(即使它需要挂钟时间)。

If you want to measure wallclock time, use time : 如果要测量挂钟时间,请使用time

time_t start = time(NULL);
sleep(3);
printf("%.2f\n", (double)(time(NULL) - start));

will print a number close to three. 将打印一个接近三个的数字。

As a side note, if you want to measure execution time in a more precise manner (milliseconds), time is not precise enough. 作为旁注,如果您想以更精确的方式(毫秒)测量执行时间,则time不够精确。 You can use gettimeofday instead: 您可以使用gettimeofday代替:

#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>

int main() {
    long start, end;
    struct timeval timecheck;

    gettimeofday(&timecheck, NULL);
    start = (long)timecheck.tv_sec * 1000 + (long)timecheck.tv_usec / 1000;

    usleep(200000);  // 200ms

    gettimeofday(&timecheck, NULL);
    end = (long)timecheck.tv_sec * 1000 + (long)timecheck.tv_usec / 1000;

    printf("%ld milliseconds elapsed\n", (end - start));

    return 0;
}

You must use time_t start = time(NULL); 你必须使用time_t start = time(NULL); and time_t end = time(NULL); time_t end = time(NULL); to get the correct values. 获得正确的值。

If you don't care about being tied to Windows, you can try the high resolution timer. 如果您不关心与Windows绑定,您可以尝试高分辨率计时器。 It's is a lot more precise than time(), which only has a precision of a single second because it the uses UNIX format. 它比time()精确得多,它只有一秒的精度,因为它使用的是UNIX格式。

#include <iostream>
#include <windows.h>

__int64 countspersec = 0;
double secpercount = 0.0;
__int64 starttime = 0;
__int64 curtime = 0;

int main() {

    // Get current time, and determine how fast the clock ticks
    QueryPerformanceCounter((LARGE_INTEGER*)&starttime);
    QueryPerformanceFrequency((LARGE_INTEGER*)&countspersec);
    secpercount = 1.0/(double)countspersec;

    /* calculate something */

    // Standard end-start stuff, account for clock speed
    QueryPerformanceCounter((LARGE_INTEGER*)&curtime);
    std::cout << "Time needed: " << (curtime-starttime)*secpercount << " sec\n";
    return 0;
}

Use QueryPerformanceFrequency() as described in Orwells answer or use the GetSystemTimeAsFileTime() function. 使用Orwells中描述的QueryPerformanceFrequency()或使用GetSystemTimeAsFileTime()函数。 The latter has 100 ns granularity but does not increment at that rate. 后者具有100 ns的粒度,但不会以该速率递增。 Its increment depends on underlaying hardware and the setting of multimedia timer resolution . 它的增量取决于硬件的底层和多媒体定时器分辨率的设置。 Keep in mind that the frequency returned by QueryPerformanceFrequency() is treated as a constant. 请记住, QueryPerformanceFrequency()返回的频率被视为常量。 However, since it is generated by hardware it has an offset and a drift in time too. 但是,由于它是由硬件生成的,因此它具有偏移和时间漂移。 Measuring periods in time by using QueryPerformanceCounter() will typically be accompanied by errors of many microseconds per second. 使用QueryPerformanceCounter()测量时间段通常会伴随每秒许多微秒的错误。 I've given this and this answer about similar matters. 我已经给出了关于类似问题的这个这个答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM