Find the answer to your Linux question:
Results 1 to 5 of 5
Hi all, I need to calculate the time of execution of a program in seconds and microseconds. I found "times" function as the right one. I used it as follows: ...
  1. #1
    Just Joined!
    Join Date
    Mar 2008
    Location
    Chennai, India
    Posts
    26

    Execution Time of a Program

    Hi all,

    I need to calculate the time of execution of a program in seconds and microseconds. I found "times" function as the right one. I used it as follows:

    #include <sys/times.h>
    #include <time.h>

    function() {

    struct tms start;
    struct tms stop;
    clock_t res;

    res = times(&start);
    if (res == -1)
    error(0, errno, "TIMES failed");
    /*
    * my application Code here
    */

    res = times(&stop);
    if (res == -1)
    error(0, errno, "TIMES failed");
    res = (stop.tms_utime + stop.tms_stime - start.tms_utime - start.tms_utime);
    printf("\ntime= %lf seconds\n", size, (double)res/CLOCKS_PER_SEC);

    }

    I tested the program as follows:

    $ time a.out <args>

    The calculated time(-0.000864 seconds) and the time displayed by "time" utility [real 0m10.398s,user 0m0.052s,sys 0m0.012s] didn't match.

    Can any one tell me what's wrong with this program?

    Thanks in advance,
    Sarma

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    You don't want to do the math as you are doing it... Basically, according to the man page for the times(struct tms*) function, it returns the timing information from some arbitrary point in time. The return value is the real time involved, so you want to use two variables for res (res1, res2) and subtract res1 from res2, keeping in mind that these are signed values and can wrap around/overflow/underflow. The CPU and User time values are NOT relevant to the realtime values as they do not reflect time the process was waiting to be scheduled.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Mar 2008
    Location
    Chennai, India
    Posts
    26
    I got it working. "ftime" saved the day!



    void function()
    {
    struct timeb start;;
    struct timeb stop;
    unsigned short res;
    ftime(&start);
    /*
    *my application code here
    */
    ftime(&stop);
    res = stop.millitm - start.millitm;
    printf("\n Time elapsed is:%u milli seconds\n", res);
    }



    Hi Rubberman,
    Do you mean that "times" is not appropriate?

  4. #4
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568
    I got it working
    Glad you got it working. Double check your results with something like gprof
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

  5. #5
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Quote Originally Posted by CSMG Sarma View Post
    I got it working. "ftime" saved the day!



    void function()
    {
    struct timeb start;;
    struct timeb stop;
    unsigned short res;
    ftime(&start);
    /*
    *my application code here
    */
    ftime(&stop);
    res = stop.millitm - start.millitm;
    printf("\n Time elapsed is:%u milli seconds\n", res);
    }



    Hi Rubberman,
    Do you mean that "times" is not appropriate?
    Sorry for the confusion. I just meant that your use of times() was incorrect. Here is an example from the man page:
    Code:
                  #include <sys/times.h>
                  #include <stdio.h>
                  ...
                  void start_clock(void);
                  void end_clock(char *msg);
                  ...
                  static clock_t st_time;
                  static clock_t en_time;
                  static struct tms st_cpu;
                  static struct tms en_cpu;
                  ...
                  void
                  start_clock()
                  {
                      st_time = times(&st_cpu);
                  }
    
                  /* This example assumes that the result of each subtraction
                     is within the range of values that can be represented in
                     an integer type. */
                  void
                  end_clock(char *msg)
                  {
                      en_time = times(&en_cpu);
    
                      fputs(msg,stdout);
                      printf("Real Time: %jd, User Time %jd, System Time %jd\n",
                          (intmax_t)(en_time - st_time),
                          (intmax_t)(en_cpu.tms_utime - st_cpu.tms_utime),
                          (intmax_t)(en_cpu.tms_stime - st_cpu.tms_stime));
                  }
    The times() function returns the real time (clock time from some arbitrary point), so to use it to get the elapsed time, you need to call it at the start of your program, saving that value to a variable. Then at the end of your program you would call it again, and subtract the first value from the new one. In your case, you were overwriting the variable. In addition, the tms structure contains cpu and userspace time values, not taking into account, as I mentioned, time the process was in a wait state.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...