开发者

How to find Time taken for doing a mathematical operation?

I am trying to do an program where i need to find the time take for doing a mathematical function. Suppose i am doing a calculation a/b, then开发者_C百科 how can i find the time taken to finish that calculation in millisecond?? Please help me


The usual process is to do it many times (say, N) then divide the time taken by that N.

For example, in pseudo-code:

count = 100000
startTime = secondsSinceStartOfDay()
for i = 1 to count
    do nothing
duration = secondsSinceStartOfDay() - startTime
print "Milliseconds per NOP = ", (duration * 1000 / count)
startTime = secondsSinceStartOfDay()
for i = 1 to count
    x = a/b
duration = secondsSinceStartOfDay() - startTime
print "Milliseconds per division = ", (duration * 1000 / count)

This allows you to discount the cost of the loop itself since you measure both with and without the operation being measured.

You do have to be careful with agressive optimisers which may decide, since you don't actually use any of the stuff you calculate, that it is free to optimise the loop code out of existence. I've actually seen this happen. Needless to say, you should understand what is happening under the covers if you're doing performance measurements.


Have a look at ctime library.

#include<ctime>
#include<time.h> 
clock_t t1=clock();
//Your calculation here
clock_t t2=clock();
cout<<"Time taken" << doubel(t2-t1) << "clock cycles";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜