How to get the running time of a program in vc6.0, that is, the number of clock cycles consumed by the program // C and C ++ Time Programming

Source: Internet
Author: User

# Include <iostream>
# Include <ctime>
Using namespace STD;
Int main ()
{
Time_t begin, end;
Begin = clock ();
// Add your code here
End = clock ();
Cout <"RunTime:" <double (end-begin)/clocks_per_sec <Endl;
}

 

UNIX time-related and standard library
In <time. h>
1. The timegm function only converts the struct TM structure to the time_t structure without the time zone information;
Time_t timegm (struct TM * TM );

2. mktime uses time zone information
Time_t mktime (struct TM * TM );

The timelocal function is a GNU extension equivalent to the POSIX function mktime.
Time_t timelocal (struct TM * TM );

3. The gmtime function only converts the time_t structure to the struct TM structure without the time zone information;
Struct TM * gmtime (const time_t * Clock );

4. Use the time zone information for localtime
Struct TM * localtime (const time_t * Clock );

1. Obtain the time and set the time in stime.
Time_t T;
T = time (& T );
2. The stime parameter should be the GMT time, which is set to the local time according to the local time zone;
Int stime (time_t * TP)

3. UTC = true indicates that the Daylight Saving Time is used;
4. The modification time and other information of the file are all stored in GMT. Different systems use localtime to convert the local time after obtaining the modification time;
5. We recommend that you use setup to set the time zone;
6. You can change the time zone to the setting in/etc/sysconfig/clock and then re-apply ln-fs/usr/share/zoneinfo/xxxx/XXX/etc/localtime.

Time_t can only represent the range of 68 years, that is, mktime can only return time_t in the range of 1970-2038
Check whether your system has time_t64, which can indicate a larger time range.

Some differences in Windows

The ctime MFC class seems to have closed time. h to a class without extension.
Ctime T = getcurrenttime ();

The systemtime structure contains millisecond information.
Typedef struct _ systemtime {
Word wyear;
Word wmonth;
Word wdayofweek;
Word wday;
Word whour;
Word wminute;
Word wsecond;
Word wmilliseconds;
} Systemtime, * psystemtime;

Systemtime T1;
Getsystemtime (& T1)
Ctime curtime (T1 );
Word MS = t1.wmilliseconds;

Systemtime interval m;
: Getlocaltime (& M );

_ Strtime () in time. h // can only be used in Windows
Char T [11];
_ Strtime (t );
Puts (t );

------------------------------------------------------------------------------
_ Timeb is defined in sys/timeb. h and has four fields.
Dstflag
Millitm
Time
Timezone

Void _ ftime (struct _ timeb * timeptr );

Struct _ timeb timebuffer;
_ Ftime (& timebuffer );
Take the current time: the document can be described in ms. Someone tests it, as if it could only be 16 Ms!

-------------------------------------------------------------------------
How to set the current system time --- windows
Systemtime m_mylocaltime, * lpsystemtime;
M_mylocaltime.wyear = 2003;
M_mylocaltime.wmonth = 1;
M_mylocaltime.wday = 1;
M_mylocaltime.whour = 0;
M_mylocaltime.wminute = 0;
M_mylocaltime.wsecond = 0;
M_mylocaltime.wmilliseconds = 0;
Lpsystemtime = & m_mylocaltime;
If (setlocaltime (lpsystemtime) // you cannot change it to setsystemtime ().
MessageBox ("OK! ");
Else
MessageBox ("error! ");

 

Systemtime m_mylocaltime, * lpsystemtime;
M_mylocaltime.wyear = 2003;
M_mylocaltime.wmonth = 1;
M_mylocaltime.wday = 1;
Lpsystemtime = & m_mylocaltime;
If (setdate (lpsystemtime) // it cannot be changed to setsystemtime ().
MessageBox ("OK! ");
Else
MessageBox ("error! ");

 

-----------------------------------------------------------------------------
Use the clock () function to obtain the millisecond-level time after the system starts. Divide the time by clocks_per_sec and replace it with the standard C function.
Clock_t clock (void );

# Include <time. h>
Clock_t T = clock ();
Long sec = T/clocks_per_sec;
It records the clock cycle, and the implementation does not seem very accurate and requires experimental verification;

---------------------------------------------------------------------------
It is said that the time structure of tc2.0 contains millisecond Information
# Include <stdio. h>
# Include <dos. h>

Int main (void)
{
Struct time t;

Gettime (& T );
Printf ("the current time is: % 2D: % 02d: % 02d. % 02d/N ",
T. ti_hour, T. ti_min, T. ti_sec, T. ti_hund );
Return 0;
}
Time is a struct, in which the member function ti_hund is haoss... The program can run on tc2.0

--------------------------------------------------------------------------------
This is a function commonly used in Windows to calculate the program running time;
DWORD dwstart = gettickcount ();

// Run your program code here

DWORD dwend = gettickcount ();

Then (dwend-dwstart) Is your program running time, in milliseconds
This function is only accurate to 55 ms, and one tick is 55 ms.
 
--------------------------------------------------------------------------------
Timegettime () is basically equal to gettickcount (), but the accuracy is higher
DWORD dwstart = timegettime ();

// Run your program code here

DWORD dwend = timegettime ();

Then (dwend-dwstart) Is your program running time, in milliseconds
Although the unit of returned values should be ms, the legend shows that the precision is only 10 ms.
--------------------------------------------------------------------------------

 

Borland C ++ builder VCL Time Functions

1. Date
The returned tdatetime object contains the current year, month, and day information. The function prototype is as follows:
System: tdatetime _ fastcall date (void );

2. Time
The returned tdatetime object contains the current time information. The function prototype is as follows:
System: tdatetime _ fastcall time (void );

3. Now
Returns the tdatetime object to obtain the current date and time information. The function prototype is as follows:
System: tdatetime _ fastcall now (void );

4. datetimetostring
Converts a tdatetime object to a string object in the specified format. The function prototype is as follows:
Void _ fastcall datetimetostring (ansistring &; Result, const ansistring format, system: tdatetime datetime );

5. datetostr
Convert the tdatetime object (including the current year, month, and day information) to a string object. The function prototype is as follows:
Ansistring _ fastcall datetostr (System: tdatetime date );

6. timetostr
Converts the current date to a string object. The function prototype is as follows:
Ansistring _ fastcall timetostr (System: tdatetime time );

7. datetimetostr
Convert a tdatetime object to a string object. The function prototype is as follows:
Ansistring _ fastcall datetimetostr (System: tdatetime datetime );

8. strtodate
Converts a String object to a year, month, or day object. The function prototype is as follows:
System: tdatetime _ fastcall strtodate (const ansistring S );

9. strtotime
Converts a String object to a time object. The function prototype is as follows:
System: tdatetime _ fastcall strtotime (const ansistring S );

10. strtodatetime
Converts a String object to an object of year, month, day, and time. The function prototype is as follows:
System: tdatetime _ fastcall strtodatetime (const ansistring S );

11. datetimetosystemtime
Convert the tdatetime object to the operating system time. The function prototype is as follows:
Void _ fastcall datetimetosystemtime (System: tdatetime datetime, _ systemtime &; systemtime );

12. systemtimetodatetime
Convert the operating system time to a tdatetime object. The function prototype is as follows:
System: tdatetime _ fastcall systemtimetodatetime (const _ systemtime &; systemtime );

 

Bytes ---------------------------------------------------------------------------------------
The following is a precise timing method for the computation.
Bytes ---------------------------------------------------------------------------------------
How can I get the running time of a program or a piece of code? You may say that there is a dedicated program testing tool, indeed, but you can also embed assembly code in the program for implementation.

In the Pentium command system, there is a command to obtain the value of the 64-bit counter inside the CPU. We can obtain the value of the counter twice by using the code to obtain the number of clock cycles for running the program or code, then, you can use the CPU frequency to calculate the time of a clock cycle, so as to calculate the exact time of the program running.

We use the tdsic command to obtain the internal counter value of the CPU. The returned value of the tdsic command is placed in edX: eax, where edX stores the high 32-bit value in the 64-bit register, eax stores the 32nd-bit value.

Let's take a look at the implementation code:

// Use assembly to obtain the running time of a piece of code
# Include <iostream>

Using namespace STD;

Void getclocknumber (long high, long low );
Void getruntime ();

Int main ()
{

Long highstart, lowstart, highend, lowend;
Long numhigh, numlow;
// Obtain the CPU internal counter value when the Code starts to run
_ ASM
{
Rdtsc
MoV highstart, EDX
MoV lowstart, eax
}
For (INT I = 0; I <100000; I ++)
{
For (INT I = 0; I <100000; I ++)
{

}

}
// Obtain the value of the CPU internal counter at the end of the code, and subtract the Initial Value
_ ASM
{
Rdtsc
MoV highend, EDX
MoV lowend, eax
; The counter value difference is obtained twice.
Sub eax, lowstart
CMP eax, 0; returns if the 32-bit low deviation is negative, because the second retrieval is always greater than the first retrieval.
JG L1
Neg eax
JMP L2
L1: mov numlow, eax
L2: SBB edX, highstart
MoV numhigh, EDX

}
// Place the difference between the two counter values in a 64-bit integer variable
// First place the 32-bit high 32-bit left shift in the 64-bit integer variable, and then add the 32-bit low
_ Int64 timer = (numhigh <32) + numlow;
// Number of clock cycles of the output code segment
// Take the frequency of 1. 1 gcpu as an example. If you change the computer, replace 1.1 with another one, because we believe everyone's CPU should be above 1 GB. ^_^
Cout <(double) (Timer/1.1/1000000000) <Endl;
Return 0;
}

In this way, you can obtain the approximate time of a program or a piece of code through a simple assembly command, but you cannot get the exact running time, even if you remove the intermediate loop, the program also has a running time,

Because after obtaining the counter value for the first time, there are two assembly commands mov highstart, EDX mov lowstart, and eax, of course, also have the running time, of course, you can subtract the running time of these two commands (3e-8s on the 1.1g host), which will be more accurate.

If you want to know the running time of the program, professional testing software will certainly be better, but it seems that there is no need to obtain programs unless specifically required.

But it is good to be DIY. Whether or not, at least you can learn how to embed assembly code in VC ++ and How to Use 32-bit registers, in fact, it is used like a 16-bit register, and the 64-bit register should be the same in the future, but the number of digits is different.

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.