Performance Timing Function

This is a discussion on Performance Timing Function within the C++ Programming forums, part of the General Programming Boards category; I am trying to time my code performance. I used clock() to time. However, it does not product precise timing. ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    25

    Performance Timing Function

    I am trying to time my code performance.

    I used clock() to time. However, it does not product precise timing. So, are there any function that I can use to get precise timing on code performance?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    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
    Nov 2006
    Posts
    25
    Quote Originally Posted by Dave_Sinkula View Post
    I found a timer in the following post.
    optimizing bubble sort

    However, I cannot use the function RDTSC, even if I copied the following:

    Code:
    #define RDTSC(llptr) { \
            __asm__ __volatile__ ( \
            "rdtsc" \
            : "=A" (llptr) \
            ); }

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    Quote Originally Posted by rosicky2005 View Post
    I am trying to time my code performance.

    I used clock() to time. However, it does not product precise timing. So, are there any function that I can use to get precise timing on code performance?
    Why do you think that clock() is imprecise? Maybe it is, but I'm wondering what evidence you have.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    25
    Quote Originally Posted by brewbuck View Post
    Why do you think that clock() is imprecise? Maybe it is, but I'm wondering what evidence you have.
    I am timing code of short runtime, for example

    Code:
    for(i = 0 to 10)
    
    data[i] = i;
    Thus, clock() is not enough to do so and I got time = 0

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    > However, I cannot use the function RDTSC, even if I copied the following:
    Well perhaps if you actually mentioned your OS/compiler (or even the error messages), then perhaps we could suggest something more suited for you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    25
    Quote Originally Posted by Salem View Post
    > However, I cannot use the function RDTSC, even if I copied the following:
    Well perhaps if you actually mentioned your OS/compiler (or even the error messages), then perhaps we could suggest something more suited for you.
    Visual Studio C++ is used in my program. The OS is window XP.

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

  9. #9
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Why do you think that clock() is imprecise? Maybe it is, but I'm wondering what evidence you have.
    With Windows, the operating system does not update the time_t structure every single millisecond. It gets updated every 30-50 milliseconds. I always forget the exact update-rate... And it might depend on what other processes are running.

    Evidence??? I don't have the reference handy, but if you write a little experimental loop that displays the time in milliseconds you will find that the same number is shown over-and-over, and then it will jump by several milliseconds, hold that value for awhile, jump again, etc.

    With Windows there are better functions (maybe GetTickCount() ) but timing is never 100% accurate on a cooperative multitasking OS like windows. (I believe you can do precise timing by writing a kernel-mode driver which can completely "take-over" the CPU.) In general, you have to use a Real Time Operating System (RTOS) for accurate short-time timing.

    In a PC, there is a dedicated hardware clock (oscillator) for all of the timing-critical stuff like audio, video, serial I/O, etc. (If you look at a soundcard or video card, you will usually see a crystal.) And, there is a buffer between the data bus and the critical-time hardware to keep the data moving smoothly when the data bus is interrupted.

    EDIT -------------------
    Quote Originally Posted by rosicky2005 View Post
    I am timing code of short runtime, for example

    Code:
    for(i = 0 to 10)
    
    data[i] = i;
    Thus, clock() is not enough to do so and I got time = 0
    ...That's going to take much less than 1ms.
    Last edited by DougDbug; 05-31-2007 at 02:51 PM.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    With Windows, the operating system does not update the time_t structure every single millisecond. It gets updated every 30-50 milliseconds. I always forget the exact update-rate...
    I think it might be every 18.2 milliseconds, but that's just a wild guess.

    Personally, if I want to time something, I often use a profiler. (A profiler counts the number of times each function in your program is called, and shows how long was taken in each function, so you know which area of code is makig your program slow.) I use gprof with GCC. I have no idea what one could use with MSVC, but I'm sure there's something out there. *googles it* Actually, it looks like a profiler comes with MSVC. http://www.google.ca/search?hl=en&q=...ler+msvc&meta= Have you tried it?

    And of course, a profiler is only useful when you're debugging a program. A final program can't use a profiler, unless you want it to run very slowly.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    I think it might be every 18.2 milliseconds...
    You could be right. That works-out to about 55 updates per second, which might be the number I was trying to pull out of the fog in the back of my brain...

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Mmm, yes, it looks like it's certainly possible: http://www.delorie.com/djgpp/v2faq/faq22_27.html
    A: Most time-related facilities in DJGPP have the same 55msec granularity of the time intervals they measure. This is because the timer tick interrupt that updates the time has the frequency of 18.2Hz. This is why calling usleep with arguments less than 55000 produces strange effects: the resolution of the argument is 1usec (for compatibility with other compilers), but the granularity is still 55msec.
    That's for DJGPP, of course, but I imagine other compilers might be similar.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Brand new to C need favor
    By dontknowc in forum C Programming
    Replies: 5
    Last Post: 09-21-2007, 10:08 AM
  3. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 07:55 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21