When you write code, you often calculate the run time of a piece of code, and the following provides a microsecond class for reference
classCtimecost { Public: Ctimecost (Const string&str): M_str (str) {QueryPerformanceFrequency (&M_freg); QueryPerformanceCounter (&m_begin); } ~Ctimecost () {Large_integer end; QueryPerformanceCounter (&end); __int64 Ntick= (end. Quadpart-m_begin. QuadPart) * +* +/M_freg. QuadPart; intLen =2048; Char*buf =New Char[Len +1]; memset (BUF,0, Len +1); sprintf_s (buf, Len,"\n%s cost%ld microseconds \ n", M_str.c_str (), Ntick); Outputdebugstringa (BUF); cout<< buf <<Endl; Delete[]buf; BUF=NULL; } Private: stringM_STR; Large_integer M_begin; Large_integer M_freg; };
How to usevoidTesttimecost () {{Ctimecost once ("This block cost"); Sleep (1); } cout<<"Other code run"<<Endl; }
C + + high-precision timekeeping under windows