Thread: [help]how to get the microsecond-precision in windows?

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    5

    [help]how to get the microsecond-precision in windows?

    hello,
    the windows API GetSystemTime() could return the current time by a SYSTEMTIME struct variable,then the windows API SystemTimeToFileTime could change this struct into the form of 100-nanoseconds that ever passed since since January 1, 1601 (UTC),this number is stored in a FILETIME struct which has a high part and a low part.
    but now I wanna get the current seconds(like the time() in time.h) and the microseconds that within one second(this is the point),
    How can I convert the 100-nanoseconds value to what I want?
    Or is there annother effective way to do this?
    thanks.
    -yogo

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    To answer your question as you asked it, there are always 1000000 microseconds in one second.

    To try to answer whatever question you are trying to ask, there are 10 100-nanosecond increments in one microsecond, so dividing your 100-nanosecond number by 10 will give you microseconds.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    62
    Im guessing you want to make a timer of some sort, correct?

    int time = GetTickCount();
    somefunc()
    time = GetTickCount() - time;

    Will give you timing in the milliseconds.

    If you need a higher resolution timer, QueryPerformanceCounter and QueryPerformanceFrequency are what you want to look into (for windows of course). They have some problems with dual core processors IIRC, but arn't too bad all together.

    If you just want a timer that can go into milliseconds, I suggest using a combination of the time function and shooting off QueryPerformanceCounter every 100 milliseconds to get the highest resolution time available.

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    QueryPerformanceCounter() is the most accurate timing available in Windows. You will also need to make use of QueryPerformanceFrequency(). Check the SDK docs.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    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.

  6. #6
    Registered User
    Join Date
    Dec 2008
    Posts
    5
    Quote Originally Posted by tabstop View Post
    To answer your question as you asked it, there are always 1000000 microseconds in one second.

    To try to answer whatever question you are trying to ask, there are 10 100-nanosecond increments in one microsecond, so dividing your 100-nanosecond number by 10 will give you microseconds.
    the 100-nanosecond is stored in a high part(unsigned long) and a low part(unsigned long),you can't join 2 parts into one number by placing the lower behind the higher,right? So I can't divide the low part by 10 to get the microseconds for this low part may be very different form the the low part of the practical 64-bit number. further,my machine dose not support __int64......
    And.....I wanna get the 100-nanoseconds within 1 second,not a pure conversion form 100-nanosecond to microsecond,that is,the part smaller than 1 second represented in microseconds.

    thanks for your reply but I couldn't work it out this way.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Actually, the LARGE_INTEGER value that you get back is a union, which also has a "quad_part", which is a 64-bit integer.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    my machine dose not support __int64......
    Your machine or your compiler? MSVC certainly supports __int64 and works just fine on my 32-bit CPU.

    Although QPC has issues timeGetTime() has far more issues. If you don't need super accurate timing then timeGetTime() will suffice. However it's precision is limited and if you try to use this for precise counters or precise deltas between times you will get hiccups. Previously I always used timeGetTime() in my game code's main loop and it resulted in stuttering and hiccups between frames leading to visual discontinuity. Since switching to QPC and QPF I do not have these issues.

    Again it all comes down to what you need. And like always neither of the solutions is 100% perfectly suited to your needs or without their disadvantages.

    BTW that was a good read Salem and is good information. Thanks for sharing.
    Last edited by VirtualAce; 12-14-2008 at 04:30 PM.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It is also possible to increase the precision of timeGetTime to about millisecond accuracy.
    But again note that polling every microsecond or millisecond is extremely expensive in terms of hardware resources. It will kick the processor out of idle mode and have it consume an unholy amount of power compared to when it is idle.
    So my advice becomes: do you truly and really need it? If yes, the limit when you use it, as to not consume too much resources, and lastly: do you need to use it when the computer is running on battery power? If no, then don't do it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Dec 2008
    Posts
    5
    hi,
    thanks for the replies,
    I've had a good lesson,this problem was finally solved after some negotiation over the way of implementation.
    thank you,Salem 'n Bubba.
    regards
    -yogo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Windows 98/2000 programming in Windows XP
    By Bill83 in forum Windows Programming
    Replies: 3
    Last Post: 07-22-2005, 02:16 PM
  2. Dialog Box Problems
    By Morgul in forum Windows Programming
    Replies: 21
    Last Post: 05-31-2005, 05:48 PM
  3. dual boot Win XP, win 2000
    By Micko in forum Tech Board
    Replies: 6
    Last Post: 05-30-2005, 02:55 PM
  4. SDL and Windows
    By nickname_changed in forum Windows Programming
    Replies: 14
    Last Post: 10-24-2003, 12:19 AM
  5. IE 6 status bar
    By DavidP in forum Tech Board
    Replies: 15
    Last Post: 10-23-2002, 05:31 PM