How to calculate the time the program runs in C + + (reprint)

Source: Internet
Author: User

Reprint Address: http://blog.csdn.net/wuxuguang123/article/details/8130081

The function of a program is usually implemented in many ways, what is the best program? For example, if you implement two programs of the same function, one button gives the result of the run, and the other takes a long time to wait, just like installing Windows XP (oh, that's too much), which program would you use? Undoubtedly, the first rule of the optimal program is that the program runs faster.

So, since a program can be implemented in a variety of ways, how do I know which method is optimal? Some programs are obvious, you run it, a program takes 3 seconds, one to 1 seconds, you can certainly feel it. If all in a second, you can see the amount of memory applied, the data size of the operation, the simple complexity of the operation process, sometimes can tell which program is the best, but some times, but it is not so easy to see, for example, it is less than the other program to define a variable, but it is more than the other program with a few division , how do you compare the speed of their operation? There's only one way--timing!

The timing function in C + + is clock (), and its associated data type is clock_t (header file is time.h). The function definition prototype is:clock_t clock (void);

This function returns the time between "opening this program process" to "calling the clock () function in the program".Number of CPU clock tick units (clock ticks),Known as the wall Clock time in MSDN (Wal-clock)。

Where clock_t is the data type used to hold the time, in the Time.h file, we can find the definition of it:
#ifndef _clock_t_defined
typedef long CLOCK_T;
#define _clock_t_defined
#endif
It is clear that clock_t is a long shaping number. In addition, a constant clocks_per_sec is defined in the Time.h file, which is used to indicate how many clock ticks are in a second, so we can use the formula clock ()/clocks_per_sec to calculate the run time of a process itself.

Here is an example to help you understand the above knowledge.

#include <iostream.h>
#include <time.h>
Void Main ()
{
   clock_t Start,finish;
   double TotalTime;
   start=clock ();

   ......                      //Insert your program code into the face

    finish=clock ();
   totaltime= (Double) (Finish-start)/clocks_per_sec;
   cout<< "\ n This program runs at <<totaltime<<" Seconds! "<<endl;
}

So, we can draw the program run time and then compare, very simple, very convenient.
Some people may ask, if two programs run for less than a second, we simply do not feel, there is a comparative significance? The answer is YES! Because we may be writing a very large program in a function, and in the main program may call this function repeatedly, if a function function than another implementation of the same function function fast 0.01 seconds, the two functions in the main function is called 1000 times, the effect is obvious.
OK, this is my little experience, I hope to give my LP and other friends to provide a little help.

       self-applied template code:

  

#include <stdio.h> #include <time.h>int f[100000001];int main () {    clock_t s, E;    s = Clock ();    ..... code ...        .. e = Clock ();    printf ("%.6lf\n", (double) (e-s)/(double) clocks_per_sec);    return 0;}

How to calculate the time the program runs in C + + (reprint)

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.