2

To measure the execution time of some portion of C++ code on Windows, I tend to use the QueryPerformanceCounter() high-resolution timer. An example of that can be found in this VCblog post on STL performance.

With the aim of writing cross platform C++ code, what functions/classes could I use for that same purpose?

Mr.C64
  • 41,637
  • 14
  • 86
  • 162
  • 1
    @LaszloPapp: Thanks, but this seems a Qt specific class. If possible, I'd like something that would not force me to introduce a dependency on Qt. Introducing a dependency on Qt would be excessive for testing/benchmarking console-mode applications written in C++, that I'd like to run on different platforms. Something from Boost would be fine. – Mr.C64 Apr 05 '14 at 10:48
  • You seem confused about what Qt is; it is an application framework, and not ui framework. It has been used just fine in console based projects. – László Papp Apr 05 '14 at 10:55
  • 1
    @Mr.C64 - so you don't want to depend on Qt, but you don't mind depending on boost? That's "funny" considering Qt provides tons of functionality far outside of the scope of boost. E.g. if you have to depend on something, then why not depend on something that enables you to do more? And at any rate, as portable as boost is, I think you will have easier time with Qt. – dtech Apr 05 '14 at 11:16
  • @LaszloPapp: I very well know what Qt is. In fact, I don't want to introduce a dependency on it for the simple need of a time-measurement class/function in C++ console mode apps. – Mr.C64 Apr 05 '14 at 11:16
  • But you want to depend on boost? I second ddriver's opinion about not understanding your logic. Not to mention, if you really want "cross-platform", you would have some pain with boost, including BC break between revisions, etc. You can also use C++11, but that is not "cross-platform" either, e.g. most of the embedded platforms will not work. – László Papp Apr 05 '14 at 11:18
  • 2
    @ddriver: Your comment is "funny". Think of Boost as a kind of "STL++", I have no problems in depending on Boost (note also that several Boost classes evolved as parts of `std::`, e.g. `boost::shared_ptr` has been promoted to `std::shared_ptr` with C++11). – Mr.C64 Apr 05 '14 at 11:20
  • I'm adult enough to know what library I want to depend on. Everyone is free to choose whatever library/framework he'd like to use. I don't want to do a flame war on that in the comments. – Mr.C64 Apr 05 '14 at 11:21
  • 1
    @Mr.C64 - I don't see anything funny about it. Boost can do very little for you compared to Qt. Boost is a mess, and as such so are TR1 and C++11 features, Qt provides alternatives with much cleaner, easier and intuitive APIs than boost. – dtech Apr 05 '14 at 11:22
  • @Mr.C64: you began the flame war. You kindly got an option and you started bitching about it for no technical reason AFAICT. If you want to avoid Qt for political reasons, put it into the question next time so that it is clear to the people who are trying to help. – László Papp Apr 05 '14 at 11:25
  • @LaszloPapp: Someone else started the flame war. I kindly replied I was not interested in Qt, and you started bitching about I don't know Qt, and someone wrote my comment was "funny", etc. I stop this thing right now. I've better things to do. – Mr.C64 Apr 05 '14 at 11:32
  • This question should go into oblivion since it is the duplicate of [C++ Cross-Platform High-Resolution Timer](http://stackoverflow.com/questions/1487695/c-cross-platform-high-resolution-timer) Please search for things like this next time. – László Papp Apr 05 '14 at 11:32
  • 1
    @LaszloPapp: There's no "political" reason for me to not use Qt in this case. I'm more than happy to use Qt in other contexts, but I don't want to introduce a dependency on Qt if there is already something in `std::` that does what I need. Moreover, if you write a polite answer writing something like "if you want to use Qt there is class Xyz for that" I'm happy to upvote that, even if @jalf's answer was what I wanted (since the class suggested by jalf is in `std::`). And I don't know who downvoted your answer on Qt solution; it was not me. – Mr.C64 Apr 05 '14 at 13:21
  • @LaszloPapp: I haven't forgotten anything. My "search path" is first `std::`, and if there is nothing in `std::` but something in Boost, then `boost::` is fine. It's not political, it's technical. Anyway, I think I've spent more than enough words on that. – Mr.C64 Apr 05 '14 at 13:27
  • **Moderator Note** Please keep your comments on-topic. Discussions about what is or is not on-topic should be taken to chat or ask a question on Meta. The comments section is not the appropriate place for this. – Taryn Apr 05 '14 at 14:29

1 Answers1

6

Assuming a modern compiler, you're probably looking for std::chrono::high_resolution_clock

jalf
  • 243,077
  • 51
  • 345
  • 550
  • This will not work without C++11 compilers, like so many on embedded, so this answer does not satisy the "cross platform C++ code" port of the question, at least completely. – László Papp Apr 05 '14 at 11:12