How can i get th execution time ??

I tried to use library <time.h>

clock_t tStart = clock();

//put here my code

double executionTime=(clock() - tStart)/CLOCKS_PER_SEC;

here the excution time always zero.

so what's the error here??

Recommended Answers

All 4 Replies

Perhaps the problem is integer division when the result is less than 1? Try casting the difference to double and see if it helps.

So the code compiles and executes without errors? It works perfectly?
In that case, the next aspect I might consider is that perhaps the section of code you are testing executes so fast, the result rounds to zero. Are you sure the code does not execute nearly instantaneously? Perhaps compute a transcendental function like sin(x) a thousand times to give it a bit of a work out. Then you should know for sure that execution is taking some time. If the result is still 0, then I am not sure what else to suggest.

thanks all

It's works when I cast the difference to the double.

double time=((double)(clock() - tStart))/(double)CLOCKS_PER_SEC;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.