Thread: Timer in C

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    28
    Rep Power
    0

    Timer in C


    Hey,

    I need to time a code in C from start to end, I was told there is a built in function in time.h library but im not quite sure which one it is and how to use it.
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Yes, it's called time() . It returns the number of seconds since the start of the epoch, which I think is 1970.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    28
    Rep Power
    0
    Thanks. How would I implement this function into the code say a loop like the following.

    void main(void)
    {
    for (i = 0, i< N, i++)
    {
    sum = sum + A[i];
    }

    return sum;

    }
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Save the time going into the loop and read it again coming out and take the difference. time.h also has functions for converting the output of time() to a tm struct translating it to date and time of day and there's a function for taking the difference between two times.

    Keep in mind that you wouldn't be able to take fractions of seconds. I've heard that utime() can take it down to milliseconds, but the man page looked like it was used for changing a file's timestamp.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    28
    Rep Power
    0
    Something like this? I tried this and it is returning numbers like 2430 for time

    void main(void)
    {
    start = time();
    for (i = 0, i< N, i++)
    {
    sum = sum + A[i];
    }

    end = time();
    time = start - end;
    return sum;

    }
  10. #6
  11. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    13
    here's what i've used to time stuff with that seems to work ok:
    Code:
    float start,total=0.0;
    start=(float)clock();
    	
    /* code you want to time */
    	
    total=total+((float)clock() - start);
    printf("\ntime: %f\n", total);
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    28
    Rep Power
    0
    Great thanks. Should I divide by CLK_TCK to get the ticks in seconds?
  14. #8
  15. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    13
    i think dividing by 100 will give it in seconds so this instead:
    Code:
    printf("\ntime: %f\n", total / 100.0);
  16. #9
  17. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Location
    in the perfect world
    Posts
    622
    Rep Power
    28
    CLOCKS_PER_SEC is the new version of CLK_TCK and both are defined as 1000. In MSVC 6 or later

    Don't expect any times to be accurate <20 - 50 millisec on a Win OS as timer messages have the lowest priority in the que.

    QueryPerformanceCounter() is a better option in WIN32 apps.
    The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

    Frank Zappa

IMN logo majestic logo threadwatch logo seochat tools logo