0
Q:

Hi
    I want to measure the time required to execute a piece of code.
For example, I want to know the time required to execute a given
function. How do I do it within C++? I know I got to use a library
called ctime. But I dont know what functions to use. Please tell me.



A:

include time.h in your C++ file

clock_t start, finish;
start = clock();


// your code here


finish = clock();


( (finish - start)/CLOCKS_PER_SEC )
should give the time taken by your code


~Madhav


I reccomend you to use this class:
http://oldmill.uchicago.edu/~wilder/Code/timer/
It is pretty simple and easy to use. I have used it in a lot of my code
snippets, works very well. It is time resolution is also high.

simply don't devide by CLOCKS_PER_SEC
(finish - start)  will give you the CPU clocks occured in between.
And I am not sure if sombody can get time detailed to less than the CPU
clock

~Madhav



| 引用(0)
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]