简体   繁体   English

用C ++测量时间

[英]Measuring time in C++

This is my c++ code. 这是我的c ++代码。

double start_time = time(NULL);
double start_clock = clock();

#pragma omp parallel for private(i) 
for(i=0;i<max_i;i++)
      PROCESS(i);

double end_time = time(NULL);
double end_clock = clock();

printf("%lf second(s)\n", end_time-start_time);
printf("%lf second(s)\n", (end_clock-start_clock)/CLOCKS_PER_SEC);

and this is the output. 这是输出。

took 2.000000 second(s)
took 11.410000 second(s)

Does anyone know why these are not consistent? 有谁知道为什么这些不一致? Is there any other way of measuring this? 还有其他测量方法吗? BTW, 2 seconds seems more reasonable based on the time I'm seeing here. 顺便说一句,根据我在这里看到的时间,2秒似乎更合理。

The clock() function returns the amount of CPU time used by your process since it started, not the absolute time according to a real-time clock. clock()函数返回自进程启动以来所使用的CPU时间量,而不是根据实时时钟返回的绝对时间。

In another comment you said that CODE_BLOCK is a parallel loop - which means in your case, it used the equivalent of 11.41 seconds of CPU time in 2 seconds of real ("wall clock") time. 在另一条评论中,您说CODE_BLOCK是一个并行循环-这意味着,在您的情况下,它使用了11秒的CPU时间,相当于2秒的实际(“挂钟”)时间。 Evidently you're using the power of about 6 CPUs in parallel. 显然,您正在并行使用大约6个CPU的功能。

This might help: 这可能会有所帮助:

int main() {
    double start_time = time(NULL);
    double start_clock = clock();

    sleep(10);

    double end_time = time(NULL);
    double end_clock = clock();

    printf("%lf second(s)\n", end_time-start_time);
    printf("%lf second(s)\n", (end_clock-start_clock)/CLOCKS_PER_SEC);
}

The output of this is: 输出为:

10.000000 second(s)
0.000070 second(s)

So, if you're calling out to the kernel in any manner, or hopping off the processor, that will only show up in one of the two timers. 因此,如果您以任何方式调用内核或跳出处理器,则只会在两个计时器之一中显示。

From the note that said you were using OpenMP: What you're likely also seeing is a multiplier effect as well. 从说您正在使用OpenMP的笔记中:您可能还会看到乘数效应。 If your openMP thread is using 8 cores, the second timer is going to count 8 times as much as the first one. 如果您的openMP线程使用8个内核,则第二个计时器的计数将是第一个计时器的8倍。

You didn't post what was in your code-block, but in general the time will vary since you're measuring the absolute time in clock ticks that your program has been running since the start of the program as well as measuring the actual amount of CPU-time your program consumed. 您没有发布代码块中的内容,但是通常情况下时间会有所不同,因为您正在测量自程序启动以来已运行的绝对时钟时间(以时钟滴答为单位),并测量实际数量程序消耗的CPU时间。 Those are two drastically different things. 那是两个截然不同的事情。

Also the values returned from clock() are not floating point values; 另外,从clock()返回的值也不是浮点值。 they should be clock_t type values, which are an integral type. 它们应该是clock_t类型值,这是整数类型。 The same is true for time() which returns a time_t type. time()返回time_t类型的情况也是如此。 Thus there is no need to assign them to floating point types until you perform some type of division and you want a floating point value as the result. 因此,在执行某种除法并且想要浮点值作为结果之前,无需将它们分配给浮点类型。

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

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