In the process of programming, it is often necessary to calculate the program execution time of key algorithms to examine the time complexity. This is a basic knowledge, but it can be deeply analyzed based on this topic. This article discusses the advantages and disadvantages of these methods and the applicable environment. It is a small functional module that provides support for future programming.
2007-10-15
Method 1: Use the clock function
Notes
The C standard allows for arbitrary values at the start of the program;
Subtract the value returned from a call to clock () at the start of
Program to get maximum portability.
Note that the time can be wrap around. On a 32bit system where
Clocks_per_sec equals 1000000 this function will return the same value
Approximately every 72 minutes.
On several other implementations, the value returned by clock () also
Provided des the times of any children whose status has been collected
Wait () (or another wait-type call). Linux does not include the times
Of waited-for children in the value returned by clock (). The Times ()
Function, which explicitly returns (separate) Information about
Caller and its children, may be preferable.
It is worth noting that the clock function is used as the timer, and the maximum is (2 ^ 32) us, that is (2 ^ 32)/1000000 S. 71.6 minutes. From this we can see that every 72 minutes is equivalent to returning to zero, that is, wrap around und.
This mechanism is used to test the CPU usage time of some running tasks. Pay attention to some details.
The template is as follows:
# Include
# Include
Int main (void)
{
Clock_t t_start;/* Start time when test starts */
Clock_t t_end;/* End Time When test ends */
T_start = clock ();/* get start time */
/* Test code */
T_end = clock ();/* Get end time */
/* Display result */
Printf ("Time: %. 3f S/N", (double) (t_end-t_start)/clocks_per_sec );
Return 0;
}
Method 2: Use the gettimeofday Function
The precision of method 1 is relatively small, in milliseconds. Gettimeofday can be used for microsecond-level timing. The basic model is like method 1. I have created a relatively late project. The main three files are as follows:
Cal_time.h
[Armlinux @ lqm include] $ cat cal_time.h
/*
* Copyright 2007 (C), Shandong University
* All Rights Reserved.
*
* Filename: cal_time.h
* Description: header file about calculating executed time
* Author: Liu Qingmin
* Version: 1.0
* Date: 2007-10-15
*/
# Ifndef cal_time_h _
# Define cal_time_h _
# Include/* gettimeofday needed */
/*
* Self-definated Data Structure
* In order to calculate the time of all functions to be tested.
*/
Typedef int (* p_func )();
/* To rely on implementation */
Extern p_func p_func_array [];
/* Module Interface */
Void cal_time (p_func p_func_array [], int size );
# Endif/* cal_time_h _*/
Cal_time.c
[Armlinux @ lqm SRC] $ cat cal_time.c
/*
* Copyright 2007 (C), Shandong University
* All Rights Reserved.
*
* Filename: cal_time.c
* Description: calculate the time of program which carries out.
* Author: Liu Qingmin
* Version: 1.0
* Date: 2007-10-15
*/
# Include
# Include
# Include
/*
* Function: cal_time
* Description: calculate the time of program which executes
* Parameters: p_func p_func_arr [] -- the function collection to be tested
* Int size -- the number of functions to be tested
* Return: None
* Notice: you cannot calculate the number of element in array which is
* Passed by value.
*/
Void cal_time (p_func p_func_arr [], int size)
{
Struct timeval start, end;
Unsigned int sum = 0;
Int I;
For (I = 0; I/* get start time */
Gettimeofday (& START, null );
/* Test code */
P_func_arr [I] ();
/* Get end time */
Gettimeofday (& End, null );
Sum + = (1000000 * (end. TV _sec-start. TV _sec)
+ (End. TV _usec-start. TV _usec ));
}
/* Print the result */
Printf ("Time: %. 6f S./N", (double) sum/1000000 );
}
Main. c
[Armlinux @ lqm SRC] $ cat main. c
/*
* Copyright 2007 (C), Shandong University
* All Rights Reserved.
*
* Filename: Main. c
* Description:
* Author: Liu Qingmin
* Version: 1.0
* Date: 2007-10-15
*/
# Include
# Include
/* Macro to calculate the number of array */
# Define array_size (x) (sizeof (X)/sizeof (x) [0])
/* Test function */
Int test1 (void );
Int Test2 (void );
P_func p_func_arr [] = {
& Test1,
& Test2
};
Int main (void)
{
Cal_time (p_func_arr, array_size (p_func_arr ));
Return 0;
}
Int test1 (void)
{
Int I;
For (I = 0; I <(1 <4); I ++ ){
Printf ("Hello/N ");
}
Return 0;
}
Int Test2 (void)
{
Int I;
For (I = 0; I <(1 <4); I ++ ){
Printf ("World/N ");
}
Return 0;
}
This mechanism is suitable for testing a large number of functions. However, the existing problems are also obvious. In this way, the test results are not very accurate. We should use the processing method of the random process to test the results several times and then take the average. This should be better. Otherwise, the test results are not scientifically comparable.
The concepts of time in Linux are not very clear and need to be further understood.