Unix/linux Environment C Programming Tutorial (+) How to get program execution time

Source: Internet
Author: User
Tags function prototype

1. Q: What can we do when we know the program execution time?

In the "C + + Application performance Optimization" book, assume that everyone read believe that you must be on the performance optimization of this piece is very important, the text is always optimized before and after the time control is very intuitive to us a feeling.

So how can we use the library functions provided by C to get the execution efficiency of an application, analyze the bottleneck of the program and make the corresponding optimization through the data.

This article explains the clock () function.

2. Let's take a look at the explanation of the clock () function in the C + + standard document

3. Function prototype clock_t clock (void);

function return value clock () returns the number of CPU clock ticks (clock ticks) between "Open this program process" to "call clock () function in program"

Returns the processor time consumed by the program.
Returns the processor time consumed by the program

4. Two important concepts need to be understood

Epoch: Point in time.

The point-in-time is an integer in standard C + +, which is represented by the time of the moment and the number of seconds (that is, the calendar time) of the standard time point.


The epoch-making system that passes the clock as a reference differs, but it is related to the execution program (usually its launch). To calculate the actual processing time of a program, the value returned by the clock should be a value that is returned by the same function that was called.
Clock tick: The timing unit for a clock is controlled by the CPU for the length of time. A clock tick is not a cycle of the CPU. Instead, it is a basic unit of time in C + +.


5.clock function

The value returned is expressed in clock ticks, which was units of time of a constant but system-specific length (with a R Elation of clocks_per_sec clock Ticks PER second).
The returned value is expressed in a clock unit, which is a constant, but system-specific length of time unit ( clocks_per_sec indicates how many clock ticks per second).
The epoch used as reference by clock varies between systems, but it's related to the program execution (generall Y its launch). To calculate the actual processing time of a program, the value returned by clock shall is compared to a value re Turned by a previous call to the same function.

The point at which the clock is taken is between different systems, it is related to program execution (usually it starts). To calculate the actual processor elapsed time for a program. The value returned by the clock should be compared to a value that once called the same function return.

Point in time

Parameters number of references

None
No

Return Value

return value

The number of the clock ticks elapsed since an epoch related to the particular program execution.

On failure, the function returns a value of -1.
Assuming failure, the function return value is-1

A word of this function is:

Start this program into the program when you call the clock () function between the CPU Clock Timing Unit ( Clock Tick ) is counted.

For example. the place to call clock is like the way we pinch the stopwatch on the sports court .

100m The running timer starts, the first one arrives at the end and the time is displayed 9.502s the second one is 9.559s

9.502s and the 9.559s is the time to race from start to finish. It's like our program starts, we pinch the stopwatch before some easy-to-create performance bottlenecks ---- call the clock () function, Then pinch the stopwatch. Evaluate the severity of the bottleneck by calculating the interval of two pinch meters.

6. Talk about clock_t
clock_t is a type defined in <ctime> as an alias of a fundamental arithmetic type.

clock_t is a type defined in the CTime header file as an alias for a basic data type.

The header file clock_t defined in the C language is time.h

We open the time.h in our own development environment and search for clock_t and we can find it.

For example, the following shows

From the above as we can know the so-called clock_t is actually a long type

7. Talk about CLOCKS_PER_SEC

Before I knew clocks_per_sec was a certain value.

Go into the time.h and look at the clock_t way to find clocks_per_sec

Presentation samples such as the following

Being able to see the clocks_per_sec is a macro that means that it will be replaced with a value of 1000 at compile time where all clocks_per_sec appear.

8. Small trial Sledgehammer

Now, let's try to test me. By writing 3 functions Testinit () testwork () Testend ()

To simulate the execution time of some modules executed by the program

#include <stdio.h>/* printf */#include <time.h>/* clock_t, Clock, clocks_per_sec */#include <m    ath.h>/* sqrt */int testinit (int n) {int num = n * n;    while (num) {--num; } return 0;}    int testwork (int n) {printf ("Begin calculating...\n");    int i,j;    int freq=n-1;                For (i=2, i<=n; ++i) for (J=SQRT (i); j>1;--j) if (i%j==0) {--freq;            Break } return freq;}    int testend (int n) {int num = n * n;    while (num) {--num; } return 0;}  int main () {clock_t T;  int F;  The first phase of the test is initialized with printf ("Begin clock...\n");  t = clock ();//The first clock () T represents the time from the start of the program to the present Moment Testinit (1500); t = Clock ()-t;//The second call clock () minus the difference of the first obtained T is two times the interval of the pinch table printf ("It took%d clicks (%f seconds) to calls Testinit (). \ n", T, ((Flo  at) t)/clocks_per_sec);   Test the second stage of work//The first clock () T represents the time from the start of the program to the moment T = Clock ();  f = testwork (99999); The second call to clock () minus the first obtained T's difference is two times the interval of the pinch table T = ClOck ()-t;  printf ("It took%d clicks (%f seconds) to call testwork () \ n", T, ((float) t)/clocks_per_sec);  printf ("The number of primes lower than 100,000 is:%d\n", f);  Test Phase III//First clock () t indicates the time from the start of the program to the present moment T = Clock ();  Testend (1255);  The second call to clock () minus the first obtained T's difference is two times the interval of the pinch table T = clock ()-t;  printf ("It took%d clicks (%f seconds) to call Testend () \ n", T, ((float) t)/clocks_per_sec); return 0;}
Output:

By comparison data we analyze the Testwork () function time-consuming, most likely the bottleneck in the project.

9. Let's take a look at how this program executes on each platform Unix/linux.

On the RHEL7.

On the RHEL6.

On the Solaris

On the Mac

Unix/linux Environment C Programming Tutorial (+) How to get program execution time

Related Article

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.