Matlab tic and toc commands implemented in C++ I need to time a few functions that I'm implementing, so I decided to implement a way of timing even multiple or sub calls based on the stack data structure and on the tic and toc commands of Matlab.

tictoc.h

#include &lt;assert.h&gt;
#include &lt;cmath&gt;
#include &lt;ctime&gt;
#include &lt;iostream&gt;
#include &lt;stack&gt;

static std::stack&lt;std::clock_t&gt; starting_times;

void start_timer()
{
    starting_times.push(std::clock());
}

int stop_timer(bool print, const char* to_execute = "")
{

    assert(!starting_times.empty());

    double milliseconds = (std::clock() - starting_times.top()) / (double)(CLOCKS_PER_SEC / 1000);
    starting_times.pop();

    if (print) {

        // minutes
        if (milliseconds &gt;= 60 * 1000) {

            double m = milliseconds / 1000 / 60;
            double s = std::fmod(m, 60);
            double ms = std::fmod(s, 1000);

            std::cout &lt;&lt; "Time elapsed '"
                      &lt;&lt; std::lrint(m) &lt;&lt; "m, "
                      &lt;&lt; std::lrint(s) &lt;&lt; "s and "
                      &lt;&lt; std::lrint(ms) &lt;&lt; "ms' "
                      &lt;&lt; to_execute &lt;&lt; "\n";

        } else if (milliseconds &gt;= 1000) {
            std::cout &lt;&lt; "Time elapsed '"
                      &lt;&lt; std::lrint(milliseconds / 1000) &lt;&lt; "s and "
                      &lt;&lt; std::lrint(std::fmod(milliseconds, 1000)) &lt;&lt; "ms' "
                      &lt;&lt; to_execute &lt;&lt; "\n";
        } else {
            std::cout &lt;&lt; "Time elapsed '" &lt;&lt; milliseconds &lt;&lt; "ms' " &lt;&lt; to_execute &lt;&lt; "\n";
        }
    }

    return milliseconds;
}


to_execute is just a string that is eventually passed to describe which functions has just stopped being executed.

Of course, as you can observe from this code, I'm not expecting the functions to last more than 60 minutes, but if you think it's a good idea to add it, feel free to suggest a way to do it.

Of course, I've already tried the code and it seems to work.

Here's a simple runnable example that could use these functions:

main.cpp

#include "tictoc.h"
#include &lt;iostream&gt;

int main(const int argc, const char** argv)
{

    start_timer();

    int sum = 0;
    for (int i = 0; i &lt; 10000; ++i) {
        sum += i;
    }

    std::cout &lt;&lt; "sum = " &lt;&lt; sum &lt;&lt; "\n";

    stop_timer(true, "to execute the sum from 0 to 999");

    return 0;
}


Now put this and tictoc.h above in the same folder and compile with C++11, assuming you have it:

g++ -std=c++11 main.cpp -o main


Run executable main as:

./main
So as you are using c++ you should use the stl for that

#include &lt;chrono&gt;
#include &lt;iostream&gt;
int main() {
    std::chrono::high_resolution_clock::time_point start, end;

    start = std::chrono::high_resolution_clock::now();    
    /* do someting */
    end = std::chrono::high_resolution_clock::now();
    double time_elapsed = std::chrono::duration_cast&lt;std::chrono::milliseconds&gt;( end - start ).count();
    std::cout &lt;&lt; "Code took " &lt;&lt; time_elapsed &lt;&lt; " miliseconds to execute\n";
};


EDIT

So here an more extended version 

#include &lt;chrono&gt;
#include &lt;iostream&gt;

std::chrono::high_resolution_clock::time_point start_timer() {
    return std::chrono::high_resolution_clock::now();
}

void stop_timer (clock::time_point &amp;start, std::string &amp;message) {
    using cast = std::chrono::duration_cast;
    auto duration = cast&lt;std::chrono::milliseconds&gt;(std::chrono::high_resolution_clock::now() - start);
    auto hours    = cast&lt;std::chrono::hours&gt;(duration).count();
    duration     -= cast&lt;std::chrono::milliseconds&gt;(hours);
    auto minutes  = cast&lt;std::chrono::minutes&gt;(duration).count();
    duration     -= cast&lt;std::chrono::milliseconds&gt;(minutes);
    auto seconds  = cast&lt;std::chrono::minutes&gt;(duration).count();
    duration     -= cast&lt;std::chrono::milliseconds&gt;(seconds);

    std::cout &lt;&lt; "The code took ";
    if (hours &gt;= 1) {
        std::cout &lt;&lt; std::lrint(hours) &lt;&lt; "h";
    }
    if (minutes &gt;= 1) {
        std::cout &lt;&lt; std::lrint(minutes) &lt;&lt; "m";
    }
    if (seconds &gt;= 1) {
        std::cout &lt;&lt; std::lrint(seconds) &lt;&lt; "s";
    }
    if (milliseconds &gt;= 1) {
        std::cout &lt;&lt; std::lrint(duration) &lt;&lt; "ms";
    }
    std::cout &lt;&lt; " to execute\n";
}


int main() {
    std::chrono::high_resolution_clock::time_point start = start_timer();    
    /* do someting */
    stop_timer(start);
}