Thread: get system precise time

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    9

    get system precise time

    HI all, I am a newbie on C++ programming

    What I need is a precise timer(to ms at least I think), is there a function which can get the system time, set time. count?

    if there is a function or class....what is the corresponding function on linux?

    Thanks a lot!

    Also hope you guys can tell me some good resource of C++ online.

  2. #2
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    >> Also hope you guys can tell me some good resource of C++ online.

    Here's a good one

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    AFAIK there is no timer in Windows that can get a resolution under 5ms.

    QueryPerformanceFrequency() and QueryPerformanceCounter() are your best timer but will still not get a very reliable time.

    Else use GetTickCount() if it is not critical.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    There's also the standard time functions..

    #include <ctime>
    std::time_t currentTime = std::time(NULL);

  5. #5
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by novacain
    QueryPerformanceFrequency() and QueryPerformanceCounter() are your best timer but will still not get a very reliable time.
    Hmm, yes they will.

    If I'm not mistaken they're equivalent to using the RDTSC intruction. And that instruction counts the clockcycles.

    On a 1GHz machine, that'd mean a precision of 1 nanosecond.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    114
    yes they use the RDTSC on some level, but then it would be better/faster to use RDTSC directly, if you know how...

    otherwise, use timeGetTime() under windows...easy to use, precision down to 5 ms by default (different on different systems, but you can set the precision to 1 ms with timeBeginPeriod(1) (if the system allows that precision!)

  7. #7
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    >>otherwise, use timeGetTime() under windows...easy to use, precision down to 5 ms by default (different on different systems, but you can set the precision to 1 ms with timeBeginPeriod(1) (if the system allows that precision!)

    Except of course that timer messages have the lowest priority in the message que and will be by-passed by even paint messages. In practice I found it impossible to get windows to time accurately
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read and set\change system time
    By Hexxx in forum C++ Programming
    Replies: 9
    Last Post: 01-02-2006, 07:11 AM
  2. Replies: 4
    Last Post: 06-13-2005, 09:03 AM
  3. Current System Time (In Milliseconds)
    By IGAU in forum C Programming
    Replies: 10
    Last Post: 03-30-2004, 06:53 PM
  4. how to ouput system date and time?
    By toaster in forum C++ Programming
    Replies: 1
    Last Post: 04-21-2002, 10:58 PM
  5. Passing system time to a function
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 02-14-2002, 01:56 PM