Thread: execution time in C , Linux

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    1

    execution time in C , Linux

    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.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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.*

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    1
    Hi..
    You can use gettimeofday() function. It is defined in sys/time.h . Just look in man page.

  4. #4
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    You can use setitimer()/getitimer() calls and write your signal handler to catch SIGALRM. You code mya look like below
    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;
    }
    Catch the signal with sigaction(), and in your signal handler increment the counter (which keeps the milliseconds or seconds values).

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by fnoyan
    You can use setitimer()/getitimer() calls and write your signal handler to catch SIGALRM.
    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.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. installing linux for the first time
    By Micko in forum Tech Board
    Replies: 9
    Last Post: 12-06-2004, 05:15 AM
  2. measuring time of execution
    By KevBin in forum C Programming
    Replies: 6
    Last Post: 12-02-2004, 02:08 PM
  3. Killing someones grandparents
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 37
    Last Post: 09-07-2003, 07:56 AM
  4. The Timing is incorret
    By Drew in forum C++ Programming
    Replies: 5
    Last Post: 08-28-2003, 04:57 PM
  5. I apologize. Good bye.
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 05-03-2002, 06:51 PM