1
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.
# Include <iostream>
# Include <windows. h>
Using namespace std;
Int main (int argc, char * argv [])
{
DWORD start, end;
Start = GetTickCount ();
For (int I = 0; I <1000; I ++)
Cout <"you are a good child! "<Endl; // your code
End = GetTickCount ()-start;
Cout <end <endl;
Return 0;
}
2
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.
# Include <iostream>
# Include <windows. h>
# Pragma comment (lib, "winmm. lib ")
Using namespace std;
Int main (int argc, char * argv [])
{
DWORD start, end;
Start = timeGetTime ();
For (int I = 0; I <100; I ++)
Cout <"you are a good child! "<Endl;
End = timeGetTime ()-start;
Cout <end <endl;
Return 0;
}
3
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;
4
# Include <iostream>
# Include <ctime> // <time. h>
Using namespace std;
Int main ()
{
Time_t begin, end;
Double duration;
Begin = clock ();
// Add your code here
End = clock ();
Duration = double (end-begin)/CLOCKS_PER_SEC;
Cout <"runtime:" <duration <endl;
}
5
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 );
6
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, it will certainly be better to test the software, 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.
Professional Author: "brucema's blog"