2

So I found this answer. The way it constructs time looks weird (and imagine it without auto), but it also doesn't work in Visual Studio 2010, because that MSVC version is too old. I don't get to chose what Visual Studio I use, so I need to stick to it. Visual studio error:

fatal error C1083: Cannot open include file: 'chrono': No such file or directory

So I had a look at the other answers, most of which declare like milion weird structures, use totally::freaky<time> factories because they are trying to get high precision time. I am pretty satisfied with time ± 5 milliseconds - I am measuring times from 100 - 5000 ms.

So how do I get approximate system/cpu millisecond time using old C++ std libraries? (MSVC2010)

PS.: In some logger files in our project, I found this:

datetimeEx(){
    time_t timer;
    time( &timer );
    tm *t = localtime( &timer );
    char tmp[32];
    sprintf(tmp, "%02d%02d%02d %02d%02d%02d", t->tm_year - 100, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
    log << tmp;
    return log;
}

It produces time like this: 2015-11-02 16:53:57:389 I played with it, but I couldn't get difference between two times.

Community
  • 1
  • 1
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
  • 3
    "using old C++ std libraries" that'll be difficult. The reason `std::chrono` was introduced in C++11 is to provide a standard cross-platform way to do this. Older methods will require using platform specific libraries like Window's QPC. – RyanP Nov 02 '15 at 16:29
  • can you use [boost](http://stackoverflow.com/questions/6734375/c-boost-get-current-time-in-milliseconds)? And personally, I feel that the new way to handle it is much clearer then the old way. It doesn't feel weird to me. – default Nov 02 '15 at 17:08
  • @Default Boost has both chrono and timer (the latter is specifically designed for measuring intervals). I was looking for a really lightweight solution that can be used in test C++ console programs which are usually about 100-300 lines. – Tomáš Zato Nov 03 '15 at 09:40

3 Answers3

1

I would probably use Boost Chrono. It formed the basis of the <chrono> that's now in the standard library, and works pretty similarly. When you can upgrade your compiler, conversion to use the standard library facilities instead should be relatively easy (in fact, in many cases it can be as simple as changing using namespace boost::chrono; to using namespace std::chrono;).

In any case, with some care about which clock you decide to use, this can meet your requirements and still leave the code reasonably portable--certainly to the major systems like Windows, Linux and Mac OS, and I'd expect (though I haven't tested personally) to most other Unixesque systems as well.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • Boost chrono works great, but getting boost to work is really pain on windows too. Actually, my question with most views is about boost being pain. – Tomáš Zato Nov 05 '15 at 08:15
0

To calculate time differences, GetTickCount() is handy. It returns the time elapsed since the system started, in milliseconds.

DWORD time1 = GetTickCount();
Something();
DWORD timediff = GetTickCount() - time1;
alain
  • 11,939
  • 2
  • 31
  • 51
0

localtime is not going to help you. It is standard C, but it only gives you up to a second resolution. gettimeofday is something available on Posix, but not on Windows. Standard answer to count time difference in windows is QueryPerformanceTimer. If you do not need that high of a resolution, you can use GetSystemTime.

SergeyA
  • 61,605
  • 5
  • 78
  • 137