Simple analysis on the function _c language of precise control time under Linux

Source: Internet
Author: User
Tags sleep
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.
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.