# Thread: how to measure time consumed at runtime

1. ## how to measure time consumed at runtime

Sorry, let me rephrase the question i had in mind...
the time <command> gives the time taken to execute the "command"..
but , i am required to find out the time between two particular instances during the execution of the program.
i tried clock() , which gives me the number of clock cycles upto that point
Code:
```   # define <time.h>

...................
....................

clock_t  time1,time2;

time1=clock();

/*   codes
codes

*/

time2=clock();```
but, it shows both time1 and time2 as being equal to 0.000000
how else should i go about ?

2. A profiler would be your best option.

>but, it shows both time1 and time2 as being equal to 0.000000
Yea, your code is probably pretty darn fast. Most of the time to get the clock trick to work, you need to repeat the code multiple times:
Code:
```start = clock();

for ( i = 0; i < 10000; i++ ) {
/* Code you want to test here */
}

/* Print the result here */```
Then once you have a result, divide it by the number of iterations and you have the result for a single run.

3. well, i got around that.. I am actually doing MPI programming.. so, even in my serial program ,i included the path to mpi so that include MPI_Wtime() that gives me the time correctly..
what was that profiler u were talking about?
are there better methods of doing this other than clock()?

4. Use an external program called a profiler. It typically tracks overhead associated wiith various actions your program makes, and the amounts of time spent in various functions. GNU gprof does a pretty good job at this, but it's nowhere near some of the commercial-grade ones which give very accurate timings.

Using time() will give a very inaccurate answer unless you perform the benchmark for minutes on end. Some APIs provide functions which can give the time to the millisecond, or even microsecond, if you're lucky. Combine it with Prelude's repeat test example and you should be able to derive fairly accurate timings quickly.

5. For those with pentium processors, use the fast clock
http://support.microsoft.com/kb/q172338/
http://cboard.cprogramming.com/showt...ighlight=rdtsc (rdtsc)