user2517676
user2517676

Reputation: 989

Measuring the Runtime of Applications in Linux

I have a bunch of applications which are compiled via their own Makefiles. Could you please let me know what can be the best way to measure their runtime? I have tried "time" command in linux but it does not have that much accuracy and stability in the result. I also tried to add clock_gettime() function, however, there were two issues associated with it. 1) The number of applications are too much to do this. 2) Even if I can add clock_gettime() to all of the applications, I do not know how to add "-lrt" to their Makefiles.

Thanks

Upvotes: 0

Views: 130

Answers (2)

Read time(7) man page.

And most importantly, run several times the program you want to benchmark. If it happens to access files (which is common), the files could be in the system cache (with a faster access).

I find the time(1) good enough. It has many options. And you should repeat at least 3 or 4 times the run. Try perhaps time yourprogram or /usr/bin/time -v yourprogram

clock_gettime(2) is only needed inside some program (which you would modify to call it). With recent kernels and libc (e.g. kernel 3.9 and libc 2.17) you don't need anymore to link -lrt to get it (it is now in libc)

You could also use gettimeofday or clock

Upvotes: 0

Prahlad Yeri
Prahlad Yeri

Reputation: 3653

I have used gettimeofday() to successfully time my executions on ubuntu:

#include <sys/time.h>

int main()
{
float msec=0;
struct timeval start,end;
char* persons[] = {"5#8","5#7","6#0","5#7"};

//printf("%d",get_height(persons));

gettimeofday(&start,NULL);
for(int z=0;z<20000;z++)
    get_height(persons);
usleep(1000000);
gettimeofday(&end,NULL);

msec = (end.tv_sec-start.tv_sec)*1000 + (end.tv_usec-start.tv_usec)/(float)1000;
printf("avg time taken: %0.2f\n",msec/(float)20000);

}

Upvotes: 1

Related Questions