[Time Measurement] How to measure the code running time [Linux/window]

Source: Internet
Author: User

1. The return unit is milliseconds.

# Include <windows. h>
DWORD dwstart = gettickcount ();
// Test code

DWORD dwtime = gettickcount ()-dwstart;


Note: gettickcount () has a limited precision, which is related to the CPU. Generally, the accuracy is around 16 ms, and the accuracy is not over 10 ms. This means that if your interval is within 16 ms, the time is reduced to 0. If it is greater than 16 ms and less than 32 ms, the time is reduced to 16 ms (not completely strict, sometimes 15 or 17, according to the CPU processing at that time ). In fact, the difference you get is that the actual time interval is divided by 16 (the specific number depends on the processing capability of your machine, but it will not be less than 10), and the remainder is discarded.


2. Return time is second
# Include <time. h>
Unsigned long start, stop;
Start = Time (null); // The value is second.
// Your program
Stop = Time (null );
Printf ("running time: % lD", stop-start );

Iii. Exact timing method

Queryperformancecounter () This function returns the value of the high-precision performance counter, which can be in subtle units
Timing. However, the minimum unit of the exact timing of queryperformancecounter () is related to the system. Therefore
You must check the frequency of the response from queryperformancecounter () to the query system.
Rate. queryperformancefrequency () provides this frequency value and returns the number of clicks per second.

The period starts when queryperformancecounter () is called for the first time.
Use the APIS provided by the window operating system. The procedure is as follows:
# Include <windows. h>

Large_integer freq; large_integer start; large_integer end;
Queryperformancefrequency (& freq); // get the clock cycle
Queryperformancecounter (& START); // get the clock count

Your program
Queryperformancecounter (& End );
/* Here * 1000, in milliseconds; * 1000000 in microseconds */
/* Because the execution time is very short (may be several microseconds), the Unit is microseconds */

Printf ("% d", (end. QuadPart-start.QuadPart) * 1000000/freq. quadpart );


Note: 1 S = 10 ^ 3 ms (milliseconds) = 10 ^ 6 μs (microseconds) = 10 ^ 9ns (nanoseconds) = 10 ^ 12 Ps (milliseconds) = 10 ^ 15fs (femtosecond) = 10 ^ 18as (a second) = 10 ^ 21zm (second) = 10 ^ 24ym (second)


Example:

# Include <stdio. h> # include <windows. h> int main () {large_integer freq; // a 64-bit signed integer. large_integer start; large_integer end; queryperformancefrequency (& freq); // obtain the "1 time/second" clock cycle and record it as Hz (HZ ). 1Hz is the queryperformancecounter (& START) every second; // get the clock count sleep (1000); // The unit of MS is queryperformancecounter (& End);/* here * 1000, in milliseconds; * 1000000 in microseconds * // * the execution time may be several microseconds ), therefore, in microseconds * // * 1 S = 10 ^ 3 ms (MS) = 10 ^ 6 μs (microseconds) = 10 ^ 9ns (nanoseconds) */printf ("% d", (end. quadPart-start.QuadPart) * 1000/freq. quadpart); Return 0 ;}

4. What if it is under the Linux platform?

There are multiple ways to measure time. One is the C language library function time (), which can provide time in seconds, relatively rough.

One is the C language library function gettimeofday (), which can be precise to a subtle level, but will be affected by CPU scheduling.

One is the time stamp counter, which can achieve the time precision in nanoseconds.

1) Time Command
-Execute commands and timing

Time find.-Name "*. c" | xargs WC-l // count the command execution time

Real 0m33. 748 s
User 0m0. 772 s
Sys 0m1. 044 s

(1) Real Time: the elapsed time from the execution of the command line to the termination of the operation;

(2) User CPU time: the user CPU time spent on command execution completion, that is, the total execution time of commands in user State;

(3) system CPU time: the total execution time of a command in the core state.

The sum of the user's CPU time and system CPU time is the CPU time, that is, the sum of the time the command occupies CPU execution. The actual time is later than the CPU time, because Linux is a multi-task operating system, and the system usually needs to process other tasks when executing a command.

Another issue that needs to be noted is that even if the same command is executed each time, the time consumed is different. The time consumed is related to system running.


2) microsecond-Level Measurement Method


Struct timeval

{

Long TV _sec; // Second Field

Long TV _usec; // subtle domain

}

(Tvend. TV _sec-tvstart. TV _sec) + (tvend. TV _usec-tvstart. TV _usec)/1000000 = timing time in seconds.

The following figure shows the time unit in microseconds.

#include <sys/time.h>#include "stdio.h"int main(){    struct timeval tstart,tend;    gettimeofday(&tstart,NULL);     for(int i=0;i<10000;++i)printf("");    gettimeofday(&tend,NULL);    long int use_useconds=1000000*(tend.tv_sec - tstart.tv_sec) + (tend.tv_usec - tstart.tv_usec);    printf("%ld\n",use_useconds);    return 0;}




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.