C ++ calculates the running time of the clock () and clock_t programs.

Source: Internet
Author: User

Class time {
PRIVATE:
Char name [20];
Double begin;
Double end;
 
Public:
Time (char *);
Time (clock_t, char *);
~ Time ();
Void start ();
Void stop ();
Void show ();
};

 

// Time. cpp member function definition header file (class implementation file)
# Include <iostream>
# Include <ctime>
# Include <cstring>
# Include "time. H"
Using namespace STD;

time: Time (char * pname)
{< br> cout <"create time Class Object" <"and start timing... " strncpy (name, pname, sizeof (name); // initialize the character array name, value assignment Class Object Name
name [sizeof (name-1)] = '\ 0 '; // assign an ending character to the last element of the character array
begin = END = 0.0; // initialize data members begin and end
}< br> time :: time (clock_t T, char * pname)
{< br> cout <"create watch Class Object" <"and start timing ...... " strncpy (name, pname, sizeof (name);
name [s Izeof (name)-1] = '\ 0';
begin = (double) T/clk_tck;
end = 0.0;
}< br> time ::~ Time ()
{< br> cout <" time Class Object" }< br> void time :: start ()
{< br> begin = (double) Clock ()/clk_tck;
}< br> void time: Stop ()
{< br> end = (double) Clock ()/clk_tck;
show ();
}< br> void time: Show ()
{< br> cout <"the timer ends. \ N consumed time: "
<(end-begin) <" seconds \ n ";
}

 

// Main. cppProgramEntry function (main function)
# Include <ctime>
# Include <conio. h>
# Include "time. H"
# Include <iostream>
# Include <cstdio>
Using namespace STD;

Void main ()
{
Time S1 ("S1 ");
S1.start ();
Getche ();
S1.stop ();
 
// Creat object using initial value
Time S2 (clock (), "S2 ");
Getche ();
S2.stop ();
 
// Creat object using initial value
Time S3 (clock (), "S3 ");
Getche ();
S3.stop ();
}

 

Running result:

 

Create Time Class Object S1 and start timing...
Timing ends.
Time consumed: 1.046 seconds

Create watch Class Object S2 and start timing ......
Timing ends.
Time consumed: 1.704 seconds

Create a watch object S3 and start timing ......
Timing ends.
Time consumed: 1.234 seconds

Analyze the time Class Object S3
Destructor time Class Object S2
Destructor time Class Object S1
Press any key to continue

 

The program describes the following:

1. clock_t

2. Clock ();

3. clk_tck

 

1. clock_t is a data type. In fact, when you open time. H is known, it is a long type, used to record the number of clocks within a period of time, that is, the CPU running unit time.

Definition in time. h: typedef long clock_t;

 

2. The clock function is defined as follows:

Clock_t clock (void );

The clock () function returns the number of CPU clock timer units (clock tick) between "enabling this program process" and "calling the clock () function in the program, in msdn, it is called the wall clock time (Wal-clock). If the wall clock time is not available,-1 is returned. Clock_t is the data type used to save the time.

 

3. You can open time. h to see the definition as follows:

*
Number of clock ticks per second. A clock tick is the unit by which
* Processor time is measured and is returned by 'clock '.
*/

# Define clocks_per_sec (clock_t) 1000.0)
# Define clk_tck clocks_per_sec

However, clk_tck seems to have been eliminated, and msdn says:

The time in seconds is the value returned by the clock function, divided by clocks_per_sec. clk_tck is equivalent, but considered obsolete.

 

Clocks_per_sec indicates the number of clock units in a second (basic unit in C/C ++). You can see that every 1‰ seconds (one millisecond), clock () is called () the value returned by the function is one,

Note:

(1) The programCodeImplemented in the C-free 4.1 Compiler (usually VC ++ 6.0 ). The getche () method is used to read characters and read a echo character from the keyboard (the letters typed on the keyboard can be displayed on the console ), this is also true in VC 6.0.

(2) If getch () is used in the C-free 4.1 compiler, the read character is echo, but paused (you need to press Enter ). While reading a character in VC ++ 6.0 does not show back (the input letter is not displayed, and the above running result is not displayed with a letter), and it runs in a coherent manner.

(3) If getchar () is used, both compilers are echo, just like the C-free getch ().

 

It cannot be said who is right, who is wrong, and the compiler is wrong. There may be many such situations in the future. I think it is safer to use VC, I have installed a visual assist x plug-in on VC. However, C-free is quite convenient and convenient, and is often used. It also uses a lot of good ide to look at my personal experience.

From: http://blog.sina.com.cn/s/blog_60be7ec80100gtdp.html

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.