Simple C++ time benchmark

It doesn’t matter which test environment you use, this simple solution will work. Even if your test environment has time bench marking, with this simple solution, you’ll be able to time any part of you test. And yes, you can also use it within the code. The code is very simple:

class TimeBenchmarker{
    clock_t start;
        const double elapsedSec = (clock()-start) / (double)CLOCKS_PER_SEC;
        cout<<"completed in "<<elapsedSec<<endl;

You can obviously replace the text message, or have it output to anywhere else you want. In order to use it, just put your code in scope, like so:

   TimeBenchmarker mark;
   results = someFunctionCall();

Upon leaving the scope the destructor will be called and you’ll get the time value. It is better than a macro, cause there are no name clashes, and you can also nest it if needed.

Leave a Reply

You must be logged in to post a comment.