Preface
Record the current time start, call the program fun(), and record the time end.
Once the time is reduced (start-end), the program run time is obtained.
First, we will introduce the most commonly used methods, but with two methods that are not very high (>=10ms):clock() and GetTickCount()
1. clock()
C system call method, the required header file ctime/, that is, both Windows and Linux can be used.
1. The return type of clock() is clock_t type
2. clock_t is actually a long type, typedef long clock_t
3. The clock() function returns the number of CPU clock timing units (wallow clock time) between when the clock() function is started in the program and when the clock() function is called in the program, the return unit is milliseconds
4. You can use the constant CLOCKS_PER_SEC, which indicates how many clock timing units there are in each second (per second).
#include <> //Introduce header filesint main() { clock_t start,end; //Define clock_t variablestart = clock(); //Start time fun() //The function that needs to be timed end = clock(); //End timecout<<"time = "<<double(end-start)/CLOCKS_PER_SEC<<"s"<<endl; //Output time (unit: s)}
2. GetTickCount()
GetTickCount() is a Windows API, and the required header file is <>.
Returns the number of milliseconds (ms) that have passed since the operating system is started. The accuracy is limited and related to the CPU. Generally, the accuracy is about 16ms, and the most accurate will not be accurate beyond 10ms. Its return value is DWORD. When the statistic flaw is too large, the result will be reduced to 0, affecting the statistical result.
#include <> //Introduce header filesint main() { DWORD t1,t2; t1 = GetTickCount(); fun() //The function that needs to be timed t2 = GetTickCount(); cout<<"time = "<<((t2-t1)*1.0/1000)<<endl; //Output time (unit: s)}
Next are two high-precision timing methods:gettimeofday() and QueryPerformanceCounter()
3. QueryPerformanceCounter()
QueryPerformanceCounter() is a Windows API, and the required header file is <>
This function returns the value of a high-precision performance counter, which can be timed in subtle units. However, the minimum unit of the exact exact timing of QueryPerformanceCounter() is the system-related,
Therefore, the system must be queryed to get the frequency of the beep sound returned by QueryPerformanceCounter(). QueryPerformanceFrequency() provides this frequency value, returning the number of beep sounds per second.
#include <> //Introduce header filesint main() { LARGE_INTEGER t1,t2,tc; QueryPerformanceFrequency(&tc); QueryPerformanceCounter(&t1); fun() //The function that needs to be timed QueryPerformanceCounter(&t2); time=(double)()/(double); cout<<"time = "<<time<<endl; //Output time (unit: s)}
4. gettimeofday()
gettimeofday() timer function in linux environment, int gettimeofday (struct timeval * tv , struct timezone * tz ), gettimeofday() will return the current time from the structure referred to by tv, and the information of the local time zone is placed in the structure referred to by tz.
//The timeval structure is defined as:struct timeval{ long tv_sec; /*Second*/ long tv_usec; /*microseconds*/ }; //The timezone structure is defined as:struct timezone{ int tz_minuteswest; /*How many minutes is the time difference between Greenwich*/ int tz_dsttime; /*Status of sunlight saving time*/ };
This function gets the time and time zone (UTC time) that have passed from January 1, 1970 to the present (according to the official linux document, the time zone is no longer used, so NULL should be transmitted normally).
Call code:
#include <sys//Introduce header filesint main() { struct timeval t1,t2; double timeuse; gettimeofday(&t1,NULL); fun(); gettimeofday(&t2,NULL); timeuse = (t2.tv_sec - t1.tv_sec) + (double)(t2.tv_usec - t1.tv_usec)/1000000.0; cout<<"time = "<<timeuse<<endl; //Output time (unit: s)}
There is also a C system call method - time(), but the accuracy is very low (second level), so it is not recommended to use it. Here we will give a little use.
time_t start,stop; start = time(NULL); fun(); stop = time(NULL);
Comes with three ways to calculate the running time of a Python code block or program
Method 1
import datetime start = () run_function(): # do something end = () print (end-start
Method 2
import time start = () run_function() end = () print str(end)
Method 3
import time start = () run_function() end = () print str(end-start)
Among them, the accuracy of Method 2 is relatively high. Method 1 is basically the worst performance. This is actually related to the system. Generally, we recommend using Method 2 and Method 3. My system is Ubuntu, which is Linux system. Method 2 returns UTC time. In many systems, the accuracy of() is very low, including Windows.
In general, it is recommended to use () in Unix systems, and it is recommended to use () in Windows systems.
Summarize
This is the article about the four commonly used timing methods for program running time in C++. For more related contents of C++ program running time timing methods, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!