Several methods of timing in C

Source: Internet
Author: User

Several methods and routines of C timing
1. Use the clock () function

Header file:<time.h>

The clock () function returns "The number of timing units (clock tick) of the CPU clock from the start of the program to the call of the function."

Each over 1ms, the count value +1
Accuracy: 1 ms
#include <stdio.h>
#include <time.h>

int main ()
{
clock_t Start,end; typedef long CLOCK_T
start = Clock ();
Long i= 1000000000l;while (i--) {}
end = Clock ();

#define CLOCKS_PER_SEC ((clock_t) 1000)
Double duration = (double) (End-start)/clocks_per_sec;
printf ("%f\n", duration); 4.015

return 0;
}
2. Use the time () function

Header file in:<time.h>

Clock () returns the number of seconds that have elapsed since 0:0:0 seconds in 1970.01.01.

That is, calendar time

Accuracy: 1 seconds
#include <time.h>

int main ()
{
time_t start,end; typedef long time_t;
Start = time (NULL); Equivalent to time (&start);
Long I=1000000000l;while (i--) {}
End = Time (NULL);

Long duration =end-start;
printf ("%ld\n", duration); 4

return 0;
}
3. Using the GetTickCount () function
Header file in:<windows.h>
In the case of high accuracy requirements, you can take advantage of the GetTickCount () function, the return value of the function is a DWORD type, indicating the time interval (up to 49.7 days) experienced by a computer in Ms. In the shorter timing, its timing error is 15ms, in the longer timing of its low timing error, if the timing is too long, as if the crash, the CPU occupancy rate is very high, can only be used in the request is not high latency program.
Accuracy: 1 ms, short-time error 15ms
#include <stdio.h>
#include <windows.h>

int main ()
{
DWORD Start,end;//typedef unsigned long DWORD;
Start = GetTickCount ();
Long I=1000000000l;while (i--) {}
End = GetTickCount ();

Double duration = (double) (End-start)/1000;
printf ("%f\n", duration); 3.922
return 0;
}
4. Using the Queryfrequencycount () function
Header file:<windows.h>
High Precision Counters
Accuracy: 1 microseconds, error not exceeding 0.5 subtlety (accuracy of 000/(CPU frequency) microseconds)
#include <stdio.h>
#include <windows.h>

int main ()
{
Large_integer F;
QueryPerformanceFrequency (&f);//Get the frequency of internal high-precision counters

Double dfreq;
Dfreq = (double) F.quadpart; Get the frequency of the counter

Large_integer Start,end;
QueryPerformanceCounter (&start);//Gets the current count value of the internal high precision counter
Long I=1000000000l;while (i--) {}
QueryPerformanceCounter (&end);

Time difference = count value difference/frequency (unit s)
Double duration = (double) (end. Quadpart-start.quadpart)/dfreq;
printf ("%f\n", duration);//3.969499
return 0;
}

Several methods of timing in C

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.