If only the test time is OK, but if the program uses the time control class functions, such as times, Gettimeofday itself will consume a lot of time, and increase the cost of program execution, so that the time is not accurate.
In this case, the function of CPU heartbeat is used to process the time, and the encapsulated function is accurate and convenient to use.
Disadvantages: Some machines because of hardware, may not support the CPU heartbeat RDTSCPLL function of the use, generally on the virtual machine can not.
Use of:When I do the set-top box test tool, use this method to control the number of connected users per second (100 users per second), the test effect is ideal, basically is every 1 seconds 100 users online.
The following function code is attached:
Copy Code code as follows:
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <asm/msr.h>
Long long g_var_llonesecjiffiescount = 0;
Long Long Getcurcpuhopcount ()
{
Long Long llcurrentcpuhopcount;
int Iaux;
RDTSCPLL (Llcurrentcpuhopcount,iaux);
return llcurrentcpuhopcount;
}
int main (int argc, char* argv[])
{
Long long llstartvalue = 0;
Long long llendvalue = 0;
struct Timeval Starttm,endtm;
int iaux = 0;
Gettimeofday (&starttm,null);
RDTSCPLL (Llstartvalue,iaux);
Sleep (3);
RDTSCPLL (Llendvalue,iaux);
Gettimeofday (&endtm,null);
G_var_llonesecjiffiescount = ((llendvalue-llstartvalue) *1000000/(endtm.tv_sec*1000000-starttm.tv_sec*1000000+ Endt
M.TV_USEC-STARTTM.TV_USEC));//Use a heartbeat to replace the wonderful
Long Long begin_time = Getcurcpuhopcount ();
Sleep (100);//The place can test some features
Long Long end_time = Getcurcpuhopcount ();
Long Long use_time = (end_time-begin_time) * 1000000/G_VAR_LLONESECJIFFIESCOUNT;
printf ("Test a feature use Time (US):%lld\n", use_time);
return 0;
}
Execution results:
Test a feature use Time (US): 100,002,362
Conclusion: This shows that the effect is still more ideal. The error is very small and can be neglected.