tags:

views:

489

answers:

5
+1  Q: 

C++ windows time

Hello,

I have a problem in using time. I want to use and get microseconds on windows using C++.

I can't find the way.

Thnaks

+1  A: 

Take a look at this: http://www.decompile.com/cpp/faq/windows_timer_api.htm

Jonas B
I am looking for microseconds not seconds
Roman Dorevich
This is the first thing said in the article I posted, so I have no idea what you're complaining aboutQ: I need a timer timer with a 1 microsecond (1,000,000th of a second) accuracy. A: You need to use the high resolution performance counter that is available in the QueryPerformanceCounter() function. It is able to provide times that exceed 1 microsecond accuracy on most hardware.
Jonas B
Oh and yes, he describes in the article how to convert it into seconds, but that doesn't mean you have to do that..
Jonas B
no. I need it for log
Roman Dorevich
+4  A: 

One popular way is using the QueryPerformanceCounter() call. This is useful if you need high-precision timing, such as for measuring durations that only take on the order of microseconds. I believe this is implemented using the RDTSC machine instruction.

There might be issues though, such as the counter frequency varying with power-saving, and synchronization between multiple cores. See the Wikipedia link above for details on these issues.

unwind
+3  A: 

Take a look at the Windows APIs GetSystemTime() / GetLocalTime() or GetSystemTimeAsFileTime().

GetSystemTimeAsFileTime() expresses time in 100 nanosecond intervals, that is 1/10 of a microsecond. All functions provide the current time with in millisecond accuracy.

EDIT:

Keep in mind, that on most Windows systems the system time is only updated about every 1 millisecond. So even representing your time with microsecond accuracy makes it still necessary to acquire the time with such a precision.

Frank Bollack
Where do you get the 100nanosecond intervals from? I've checked through those docs and can't see any indication that the resolution is as you say, in fact the indication is that the resolution is 10-16 milliseconds (see http://msdn.microsoft.com/en-us/library/ms725496%28VS.85%29.aspx)
RobS
+5  A: 

You can use boost date time library.

You can use boost::posix_time::hours, boost::posix_time::minutes, boost::posix_time::seconds, boost::posix_time::millisec, boost::posix_time::nanosec

http://www.boost.org/doc/libs/1%5F39%5F0/doc/html/date%5Ftime.html

Davit Siradeghyan
no boost using at our system
Roman Dorevich