Timers

This is a discussion on Timers within the Game Programming forums, part of the General Programming Boards category; What is the most accurate, best get time function? I used to use GetTickCount(), but it is incredibly inaccurate, and ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    22

    Timers

    What is the most accurate, best get time function? I used to use GetTickCount(), but it is incredibly inaccurate, and timeGetTime() is just as bad. Any suggestions?

    -Vulcan

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4

    Cool

    Here are the functions from Dhonn's hi-res timer:


    // Global variables
    // frametime will return the time between frames
    // 1.0 / frametime will give the fps
    double rate_inv, startclock, start_time, end_time, frametime;


    // Initialise hi-res timer
    bool initftime(void)
    {
    __int64 rate;

    // we need the accuracy
    if(!QueryPerformanceFrequency((LARGE_INTEGER*)&rat e))
    {
    return FALSE; // win errors
    }

    // usually the rate will be 1193180
    if(!rate)
    {
    return FALSE;
    }

    rate_inv=1.0/(double)rate;

    if(!QueryPerformanceCounter((LARGE_INTEGER*)&start clock))
    {
    return FALSE; // win errors
    }

    return TRUE; // there is a clock
    }

    double ftime(void)
    {
    // by dividing by its rate you get accurate seconds

    __int64 endclock;

    QueryPerformanceCounter((LARGE_INTEGER*)&endclock) ;

    return (double)(endclock-startclock)*rate_inv;

    // note: I recommend that you multiply but the inverse of a constant.
    // (speed reasons)
    }

    // Returns the time between frames (assuming this function is
    // called every frame.
    void GetFrameTime(void)
    {
    do
    {
    start_time = ftime();
    } while(start_time==end_time);

    // the total time it took to put together the last frame
    // and get back here...

    frametime = start_time - end_time;
    end_time = start_time;
    }

    It works great most of the time, but I've had problems with it being choppy in XP.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    I use timeGetTime() for everything, just remember it returns the number of miliseconds since the machine was started in an integer number.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. timers in c
    By steve1_rm in forum C Programming
    Replies: 10
    Last Post: 02-24-2009, 12:34 PM
  2. Threads and Timers
    By scioner in forum C Programming
    Replies: 8
    Last Post: 03-22-2008, 08:56 AM
  3. Timers, Timers, Timers!
    By Stan100 in forum Game Programming
    Replies: 9
    Last Post: 01-24-2003, 04:45 PM
  4. Timers
    By Mox in forum Windows Programming
    Replies: 2
    Last Post: 11-09-2001, 04:34 AM

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