views:

1962

answers:

10

I'm looking to implement a simple timer mechanism in C++. The code should work in Windows and Linux. The resolution should be as precise as possible (at least millisecond accuracy). This will be used to simply track the passage of time, not to implement any kind of event-driven design. What is the best tool to accomplish this?

A: 

The first answer to C++ library questions is generally BOOST: http://www.boost.org/doc/libs/1%5F40%5F0/libs/timer/timer.htm. Does this do what you want? Probably not but it's a start.

The problem is you want portable and timer functions are not universal in OSes.

jmucchiello
+2  A: 

The StlSoft open source library provides a quite good timer on both windows and linux platforms. If you want it to implement on your own, just have a look at their sources.

Malte Clasen
A: 

I am not sure about your requirement, If you want to calculate time interval please see thread below

http://stackoverflow.com/questions/1468596/c-programming-calculate-elapsed-time-in-milliseconds-unix

sat
+9  A: 

Boost.Timer might work, but it depends on the C function clock and so may not have good enough resolution for you.

Boost.Date_Time includes a ptime class that's been recommended on Stack Overflow before. See its docs on microsec_clock::local_time and microsec_clock::universal_time, but note its caveat that "Win32 systems often do not achieve microsecond resolution via this API."

STLsoft provides, among other things, thin cross-platform (Windows and Linux/Unix) C++ wrappers around OS-specific APIs. Its performance library has several classes that would do what you need. (To make it cross platform, pick a class like performance_counter that exists in both the winstl and unixstl namespaces, then use whichever namespace matches your platform.)

Josh Kelley
+1  A: 

The ACE library has portable high resolution timers also.

Doxygen for high res timer:
http://www.dre.vanderbilt.edu/Doxygen/5.7.2/html/ace/a00244.html

A: 

I have seen this implemented a few times as closed-source in-house solutions .... which all resorted to #ifdef solutions around native Windows hi-res timers on the one hand and Linux kernel timers using struct timeval (see man timeradd) on the other hand.

You can abstract this and a few Open Source projects have done it -- the last one I looked at was the CoinOR class CoinTimer but there are surely more of them.

Dirk Eddelbuettel
+2  A: 

This isn't the greatest answer, but here are some conversations on a Game Development site regarding high resolution timers:

  1. http://www.gamedev.net/community/forums/topic.asp?topic%5Fid=374327
  2. http://www.gamedev.net/community/forums/topic.asp?topic%5Fid=471804
  3. http://www.gamedev.net/community/forums/topic.asp?topic%5Fid=40600
Eric Palakovich Carr
A: 

I highly recommend boost::posix_time library for that. It supports timers in various resolutions down to microseconds I believe

Maciek
+1  A: 

STLSoft have a Performance Library, which includes a set of timer classes, some that work for both UNIX and Windows.

JamieH
+2  A: 

Matthew Wilson's STLSoft libraries provide several timer types, with congruent interfaces so you can plug-and-play. Amongst the offerings are timers that are low-cost but low-resolution, and ones that are high-resolution but have high-cost. There are also ones for measuring pre-thread times and for measuring per-process times, as well as all that measure elapsed times.

There's an exhaustive article covering it in Dr. Dobb's from some years ago, although it only covers the Windows ones, those defined in the WinSTL sub-project. STLSoft also provides for UNIX timers in the UNIXSTL sub-project, and you can use the "PlatformSTL" one, which includes the UNIX or Windows one as appropriate, as in:

#include <platformstl/performance/performance_counter.hpp>
#include <iostream>

int main()
{
    platformstl::performance_counter c;

    c.start();
    for(int i = 0; i < 1000000000; ++i);
    c.stop();

    std::cout << "time (s): " << c.get_seconds() << std::endl;
    std::cout << "time (ms): " << c.get_milliseconds() << std::endl;
    std::cout << "time (us): " << c.get_microseconds() << std::endl;
}

HTH

dcw