C + + high-precision performance test function

Source: Internet
Author: User

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 (&AMP;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 (&AMP;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.