来计算程序执行时间

来计算程序执行时间

本文介绍了使用时钟()来计算程序执行时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是这样的事情算多久需要我的程序从开始到结束:

  INT的main(){
    clock_t表示startClock =时钟();
    .... //很多codeS
    clock_t表示endClock =时钟();
    的printf(%LD(endClock - startClock)/ CLOCKS_PER_SEC);
}

我的问题是,因为有在同一时间运行多个程序,说如果时间我的过程是在X数量的闲置,德宁时间会时钟我的计划之内打勾

所以基本上我关心的是,说有路过1000个时钟周期,但我的过程中只使用其中的500,我将获得500或1000从(endClock - startClock)

感谢。


在POSIX
解决方案

时钟测量CPU的时间,但它通常具有分辨率极差。相反,现代的程序应该使用 clock_gettime CLOCK_PROCESS_CPUTIME_ID 时钟-ID。这将放弃纳秒解析结果,通常它真的只是那么好。

I'm using something like this to count how long does it takes my program from start to finish:

int main(){
    clock_t startClock = clock();
    .... // many codes
    clock_t endClock = clock();
    printf("%ld", (endClock - startClock) / CLOCKS_PER_SEC);
}

And my question is, since there are multiple process running at the same time, say if for x amount of time my process is in idle, durning that time will clock tick within my program?

So basically my concern is, say there's 1000 clock cycle passed by, but my process only uses 500 of them, will I get 500 or 1000 from (endClock - startClock)?

Thanks.

解决方案

clock on POSIX measures cpu time, but it usually has extremely poor resolution. Instead, modern programs should use clock_gettime with the CLOCK_PROCESS_CPUTIME_ID clock-id. This will give up to nanosecond-resolution results, and usually it's really just about that good.

这篇关于使用时钟()来计算程序执行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 06:32