Results 1 to 13 of 13

Thread: timing

  1. #1

    Thread Starter
    Addicted Member jmiller's Avatar
    Join Date
    Jul 2002
    Location
    University of Michigan
    Posts
    238

    Question timing

    Is there a way to access the system tick count, or some other sort of timer, in C++. I'm very new to the language.
    thanks,
    jmiller

  2. #2
    New Member
    Join Date
    Sep 2002
    Posts
    12
    A very easy timer class that I make use of is CPerfTimer.

    It can be found at the following link:

    http://www.codeproject.com/datetime/perftimer.asp

    You can simply just do:

    CPerfTimer X = 0; // X is now a timer object and is set to 0 seconds.

    X.Start() // X will now begin to tick

    while (X < 3.1415926535) {} // just pause until X is greater than or equal to PI.

    X.Stop() // stop the timer X

    X = 0 // reset it to 0 seconds.

    Seems easy enough to me and it's VERY precise.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    This class is internally making use of the multimedia hardware timer. This timer can be accessed through the QueryPerformanceCounter function.
    If you don't need that precision, GetTickCount will give you the milliseconds since the start of windows and is, unlike the multimedia timer, available on all systems.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4

    Thread Starter
    Addicted Member jmiller's Avatar
    Join Date
    Jul 2002
    Location
    University of Michigan
    Posts
    238
    What library has the GetTickCount function in it?

  5. #5

    Thread Starter
    Addicted Member jmiller's Avatar
    Join Date
    Jul 2002
    Location
    University of Michigan
    Posts
    238
    Maybe that's the wrong question. GetTickCount is a API call, right? How do you make the call for the function?

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Yep, API.
    The prototype is simple:
    DWORD GetTickCount(void);

    DWORD is a typedef for unsigned long.

    So you can do

    ticks = GetTickCount();
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You can also use the ultra-high-resolution timestamp counter built into Pentiums and up (I believe most modern AMD chips have the same).

    It needs a bit of ASM though...search for RTDSC (or RDTSC, can never remember the order) on Google.

    I would post my code but it's currently archived somewhere with no hope of getting it quickly
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  8. #8
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    the clock() function accesses a high res timer, and works on any system - it's part of standard C/C++.

    usage:
    Code:
    #include <time.h>
    clock_t start;
    
    start=clock();
    ........................
    
    printf("Elapsed time %u\n",clock()-start);

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The clock() resolution may be highly hardware dependent and is not necessarily high. The clock() function has a resolution of CLOCKS_PER_SEC ticks per second. This constant is hardware and implementation-dependent.

    The MS implementation of the CRT running under windows has a resolution of 1000 ticks/second, which is equal to the resolution of GetTickCount. The difference between the two is that GetTickCount retrieves the number of milliseconds since the start of windows whereas clock retrieves the number of ticks since the start of your app.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    clock is standard instrumentation. ie., portable.

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Correct. That's probably the main difference. I just wanted to point out that clock is not very high res.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  12. #12
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    For the HP box I do a fair amount of work on
    Code:
    #ifndef CLOCKS_PER_SEC
    #define CLOCKS_PER_SEC 1000000
    #endif
    Under windows it has the value 1000:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void sleep( clock_t wait );
    
    void main( void )
    {
       long    i = 600000L;
       clock_t start, finish;
       double  duration;
    
       /* Delay for a specified time. */
       printf( "Delay for three seconds\n" );
       sleep( (clock_t)3 * CLOCKS_PER_SEC );
       printf( "Done!\n" );
    
       /* Measure the duration of an event. */
       printf( "Time to do %ld empty loops is ", i );
       start = clock();
       while( i-- ) 
          ;
       finish = clock();
       duration = (double)(finish - start) / CLOCKS_PER_SEC;
       printf( "%2.1f seconds\n", duration );
    }
    
    /* Pauses for a specified number of milliseconds. */
    void sleep( clock_t wait )
    {
       clock_t goal;
       goal = wait + clock();
       while( goal > clock() )
          ;
    }
    It has other uses as well.

  13. #13
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    As I said, highly hardware and implementation dependent.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

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



Click Here to Expand Forum to Full Width