Two methods for calculating program running time in Linux
Source: Internet
Author: User
Two methods for calculating the program running time in Linux: general Linux technology-Linux programming and kernel information. For details, refer to the following section. 1. Here is what I see online:
Sometimes we need to calculate the execution time of the program. For example, we need to analyze the algorithm time.
... You can use the following function at this time.
# Include
Int gettimeofday (struct timeval * TV, struct timezone * tz );
Strut timeval {
Long TV _sec;/* seconds */
Long TV _usec;/* Number of microseconds */
};
Gettimeofday stores the time in the Structure TV. tz Is generally replaced by NULL.
# Include # Include # Include Void function ()
{
Unsigned int I, j;
Double y;
For (I = 0; I <1000; I ++)
For (j = 0; j <1000; j ++)
Y = sin (double) I );
}
Main ()
{
Struct timeval tpstart, tpend;
Float timeuse;
Gettimeofday (& tpstart, NULL );
Function ();
Gettimeofday (& tpend, NULL );
Timeuse = 1000000 * (tpend. TV _sec-tpstart. TV _sec) +
TV _usec-tpstart. TV _usec;
Timeuse/= 1000000;
Printf ("Used Time: % f \ n", timeuse );
Exit (0 );
}
This program outputs the execution time of the function. We can use this to test the system performance or calculate the function.
The output result on my machine is: Used Time: 0.556070.
2. The second is what I often use:
Add time before executing the program, for example, input time./abc.
I don't know why the first method is widely introduced on the internet ......
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