I really do not want to profile because I wanted to do a lot of small benchmark on various simple tasks. I have no way of recording the amount of milliseconds in C ++ for life, I am using Linux on the way.
Can you find the system clock in milliseconds (I can not find an easy way if I can with seconds ..) and what are included in the header? Using gettimeofday
function from sys / time.h
Code> header file, I use this class:
#include & lt; Cstdlib & gt; # Include & lt; Sys / time.h> Class timer {time-time timer [2]; Public: Timeline Start () {gettimeofday (and this-> Timer [0], NULL); This-> timer [0]; } Timeout () {gettimeofday (& amp; it-> timer [1], NULL); This-> timer [1]; } Int duration () const {int sec (it-> timer [1] .tv_sec - this-> timer [0] .tv_sec); Integer user (this-> timer [1] .tv_usec-this-> timer [0] .tv_usec); If (usecs & lt; 0) {--secs; Usecs + = 1000000; } Return static_cast & lt; Int & gt; (Sec * 1000 + usecs / 1000.0 + 0.5); }};
For example:
#include & lt; Iostream & gt; #include & lt; String & gt; # Include & lt; Sstream & gt; Int main () {Timer TM; Std :: ostringstream ooo; Std :: string str; Tm.start (); For (Int i = 0; I <10000000; ++ i) {ooo & lt; & Lt; "This is a string."; } Tm.stop (); Std :: cout & lt; & Lt; "Std :: ostingstream ->" & Lt; & Lt; Tm.duration () & lt; & Lt; Std :: endl; Tm.start (); For (Int i = 0; I <10000000; ++ i) {str + = "This is a string."; } Tm.stop (); Std :: cout & lt; & Lt; "Std :: string - & gt;" & Lt; & Lt; Tm.duration () & lt; & Lt; Std :: endl; }
Comments
Post a Comment