简体   繁体   English

C程序的计算执行时间错误?

[英]error in calculation execution time of c program?

can anyone tell me what will happen if I miss double in below lines? 谁能告诉我如果我在以下几行中错过双打会怎样?

time_spent = (double)(end - begin) / CLOCKS_PER_SEC;

I was trying to calculate running time of my sorting problem, and forgot to typecast to double. 我试图计算排序问题的运行时间,却忘了打字以加倍。 The code ran for almost 90 mins, but the output time printed was "270.000006". 该代码运行了将近90分钟,但是输出的输出时间为“ 270.000006”。 Can anyone please help me in figuring out what this 270 signifies?? 谁能帮助我弄清楚这270代表什么?

Also it is sometimes showing "-ve" values. 此外,有时还会显示“ -ve”值。 Any solution for this problem. 任何解决此问题的方法。

EDIT - I am sorting 10^9 and 10^10 numbers, so code will be running for hours. 编辑-我正在对10 ^ 9和10 ^ 10的数字进行排序,因此代码将运行数小时。

If the code runs for almost 90 minutes, you will be getting an overflow of clock_t type on 32-bit architecture in under 72 minutes already. 如果代码运行了将近90分钟,那么您将在不到72分钟的时间内获得32位体系结构上clock_t类型的溢出。 I do believe that this is your case. 我确实相信这是您的情况。

Correct usage is: 正确用法是:

    clock_t begin, end;
    double time_spent;

    begin = clock();
    /* actual task that needs to be monitored */
    end = clock();
    time_spent = (double)(end - begin) / CLOCKS_PER_SEC; // in seconds

You are measuring CPU time here, not the elapsed time which includes I/O time as well. 您在此处测量的是CPU时间,而不是包括I / O时间在内的经过时间。 CLOCKS_PER_SEC is a constant which is declared in . CLOCKS_PER_SEC是在中声明的常量。 The computation needs to be done in floating point arithmetic. 该计算需要以浮点算法进行。 Another alternative to compute timing is to use the time command. 计算时序的另一种方法是使用时间命令。

Read How to log the time taken for a unix command? 阅读如何记录unix命令花费的时间?

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

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