Search code examples
c++timetimer

matlab tic toc equivalent in C++


I have searched but I can't find an equivalent to the matlab tic/toc function to simply display on the console how long time took the program to do its processing. (ideally I would like to put the tic (start timer) and toc (end timer) anywhere in the program.

Any suggestions?


Solution

  • I found what I was looking for. Include:

    #include <ctime>
    

    Then at the beginning:

     time_t tstart, tend; 
     tstart = time(0);
    

    And finally before the end:

    tend = time(0); 
    cout << "It took "<< difftime(tend, tstart) <<" second(s)."<< endl;