The shell has a built-in time
command. If you use that, it will display wall clock as well as user and system times.
linux> /bin/sh
$ time a.out
0m1.01s real 0m0.00s user 0m0.00s system
If you need a better resolution than 0.01 second, you can use getrusage
and gettimeofday
.
When profiling a function, it is best to place instrumentation as near the function as possible. Using something like valgrind will most likely mask what is going on within the function.
The signal to noise ratio of the measurement has to be high for it to be useful.
linux> cat a.c
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/resource.h>
/*
* Time elapsed in milli-seconds. Tweak this to get micro-seconds.
*/
int
time_diff(struct timeval *t2, struct timeval *t1)
{
int r;
r = (t2->tv_sec - t1->tv_sec)*1000 + t2->tv_usec/1000 - t1->tv_usec/1000;
return(r);
}
int
main()
{
struct rusage r1, r2;
struct timeval t1, t2;
(void)gettimeofday(&t1, 0);
(void)getrusage(RUSAGE_SELF, &r1);
sleep(1); /* Function to be profiled. */
(void)getrusage(RUSAGE_SELF, &r2);
(void)gettimeofday(&t2, 0);
printf("real = %d (ms), user = %d (ms), system = %d (ms)\n",
time_diff(&t2, &t1),
time_diff(&r2.ru_utime, &r1.ru_utime),
time_diff(&r2.ru_stime, &r1.ru_stime));
return(0);
}
When you run the above, you should see something similar to below.
linux> a.out
real = 1000 (ms), user = 0 (ms), system = 0 (ms)