Calculation of program running time in VC/MFC

Source: Internet
Author: User

In our actual programming work, we often need to measure the running time of the program, such as the running time of the algorithm. Here I collected methods for online centralized measurement of program running time.

Check the information on the Internet and find the following methods for obtaining the program running time in VC:

Method 1 using the gettickcount function (MS)

Code:

Cstring
STR;        
 

Longt1 = gettickcount (); // gets the system running time (MS) before the start of the program segment)           

...... // To do something

Longt2 = gettickcount (); // gets the system Runtime (MS) after the program segment ends)       

Str. Format ("Time: % DMS", t2-t1); // The difference between front and back is the running time       

Afxmessagebox (STR );

 

Method 2 use the C/C ++ timing function (s)

Code:

# Include "time. H"

Clock_t Start, Finish;

Start= Clock ();

Finish
=
Clock ();

Printf ("% F seconds \ n", (double) (finish-Start)/clocks_per_sec );

Method 3Use the ctime class to obtain the system time

Code:

Cstring STR;

// Obtain the system time

Ctime TM;

TM = ctime: getcurrenttime ();

STR = TM. Format ("the current time is % Y, % m, % d.% X");

Afxmessagebox (STR );

Method 4Use the getlocaltime class to obtain the system time

Systemtime st;

Cstring strdate, strtime;

Getlocaltime (& St );

Strdate. Format ("m ----", st. wyear, st. wmonth, st. wday );

Strtime. Format ("-:-", st. whour, st. wminute, st. wsecond );

Afxmessagebox (strdate );

Afxmessagebox (strtime );

 

Method 5 Use API functions

BOOL QueryPerformanceCounter(  LARGE_INTEGER *lpPerformanceCount   // counter value);

 

 

I have found these methods. If you have any new methods, share them with you.

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.