In the actual software development work, we often test the execution efficiency of a module or function. Or is the time complexity of an algorithm (although the time complexity depends partly on the machine performance, but on the same computer, the algorithm is optimized to test its complexity); It takes time precisely to get the exact run time, the following function realizes the precise timing, The timing accuracy can be achieved in microseconds, and can be used to test the efficiency of a module!
Purpose:this programme is designed for testing the performance of your code, some function ect,//it can show the time O F spending on your running on it. It is beneficial to improve your performance//of code//author:jackery_shh//data:july 8th 2015#include<windows.h& GT, #include <iostream> #include <tchar.h>using namespace std; #define IN#IFDEF _DEBUG #define _trace_size 500inline void Filetrace (in Lpctstr szformat, ...) {if (Szformat = = NULL) return; TCHAR T_trace[_trace_size] = {_t ('% ')};va_list VL = null;//generated content Va_start (VL, Szformat); _vsntprintf_s (T_trace, _trace_s Ize, _truncate, Szformat, VL); Va_end (VL); OutputDebugString (t_trace);} #elseinline void Filetrace (in Lpctstr szformat, ...) {} #endifLARGE_INTEGER Begin_time, Freq;int Main () {Large_integer begin_time, freq; QueryPerformanceFrequency (&freq); QueryPerformanceCounter (&begin_time); Begin time for (long test0=0;test0<5000000;test0++) {test0=test0+1; }large_integer END_TIME0; QuerypErformancecounter (&END_TIME0);//after runnin the ' for ' Loop statement,the time is TIME0 double TIME0 = ((double) (end _TIME0. Quadpart-begin_time. QuadPart) (*1000.0)/(double) (freq. QuadPart); int Test1=10000;while (test1) {test1--;} Large_integer end_time1; QueryPerformanceCounter (&END_TIME1); Double Time1 = ((double) (end_time1. Quadpart-begin_time. QuadPart) (*1000.0)/(double) (freq. QuadPart); Filetrace (_t ("*********************************************************************************\n")); Filetrace (_t ("Test result is as follow: \ntime:%i64x,%.3fms,%.3fms\n"), (ulonglong) 0, TIME0, Time1); Filetrace (_t ("*********************************************************************************\n")); System ("pause"); return 0;}Test result is:
C + + high-precision performance test function