hi
I am interested in calculating execution time of my program, in resolution less than ms. i tried time() function in time.h but it is not working.
is there any function in C, Linux which i can use for this.
hi
I am interested in calculating execution time of my program, in resolution less than ms. i tried time() function in time.h but it is not working.
is there any function in C, Linux which i can use for this.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
Hi..
You can use gettimeofday() function. It is defined in sys/time.h . Just look in man page.
You can use setitimer()/getitimer() calls and write your signal handler to catch SIGALRM. You code mya look like below
Catch the signal with sigaction(), and in your signal handler increment the counter (which keeps the milliseconds or seconds values).Code:long int usec = 0; void *sig_handler(int sig) { usec++; setitimer(...); } int main() { struct itimer ...; /* initialize your data structures to rise a signal once in a sec or what ever you want */ setitimer(...); /* do your work here */ getitimer(...); printf(fields in your time structures); return 0; }
Raising a signal multiple times during program execution would affect the timing result (especially if the OP is looking at such a high timing resolution). Calling gettimeofday() once at the beginning of the program and once at the end is a much wiser decision. Then you just need to calculate the difference between the timeval structs.Originally Posted by fnoyan
If you understand what you're doing, you're not learning anything.