18. in-depth understanding of computer system notes: measuring program execution time

Source: Internet
Author: User

1People cannot perceive shorter than or equal100 ms.

2The computer has an external timer that periodically sends interrupt signals to the processor. The time between these interrupt signals is called the interval (Interval Time).

3Switching from one process to another requires thousands of clock cycles to save the status of the current process. The typical timer interval range is1~10 ms.

4, Count by interval (Interval counting) To measure the time

OSAlso use the timer (Timer) To record the cumulative time used by each process.ProgramThe execution time is less precise. This method only applies to a long duration (to a small1 s.

OSMaintains the Count value of the user time and system time used by each process.

Read the process counter using the following two methods:

1)ShellAddTime.

The first two values are the user and system time of the command process execution, and the third value is the total time of execution.

2) Call a functionTimesTo read the timer of the process.

ExampleCode

# Include <sys/times. h> struct TMS {clock t TMS utime;/* User time */clock t TMS stime;/* system time */clock t TMS cutime; /* User time of reaped children */clock t TMS cstime;/* system time of reaped children */} clock t times (struct TMS * BUF); returns: number of clock ticks elapsed since system started # include <time. h> clock t clock (void); returns: Total time used by Process

These time measurements are based on the clock tick (Clock tick. Defined constantClk_tckIndicates the number of clock ticking times per second.The fields indicating child times give

Accumulated times used by children that have terminated and have been reaped .(It means that each subdomain provides a subtime item that we may need. These subtime items are accumulated by subprocesses that have been terminated and recycled.).

Therefore,TimesIt cannot be used to monitor the time used by any ongoing sub-process. As the return value, the total number of tick messages that have been received since the system was started.

3 ) ansi c defines a clo CK function to measure the total time used by the current process. clock function report time changes to seconds, to divide by the defined constant clocks_per_sec , this constant is not necessarily consistent with clk_tck same.

5And uses the period counter to measure the time.

CPUThere is a special timer on, each clock cycle plus1.

This book9.3.1This section describes how to use the assembly code to obtain the counter value of this cycle. A good measurement value can be obtained at the micro scale.

I also talked aboutKSub-optimal measurement method.

6, PassGettimeofdayFunction Measurement

GettimeofdayFunction query system clock (System clock) To determine the current date and time.

Sample Code

 
# Include "time. H "struct timeval {long TV sec;/* seconds */long TV usec;/* microseconds */} int gettimeofday (struct timeval * TV, null); returns: 0 for success,-1 for failure

InLinuxThe second parameter is setNullBecause it points to an unimplemented feature for performing time zone correction.

Sample Code

# Include <stdlib. h> # include <stdio. h> # include <time. h>/* $ begin Todd */# include <sys/time. h> # include <unistd. h> static struct timeval tstart;/* record current time */void start_timer () {gettimeofday (& tstart, null );} /* get number of seconds since last call to start_timer */Double get_timer () {struct timeval tfinish; long sec, USEC; gettimeofday (& tfinish, null); sec = tfinish. TV _sec-tstart. TV _sec; USEC = tfinish. TV _usec-tstart. TV _usec; return sec + 1e-6 * USEC;}/* $ end Todd */

Implementation depends onGettimeofdayHow is it implemented, andGettimeofdayThe implementation varies with the system.

7For more information about the measurement methods and techniques, see9.4, 9.5, 9.6, 9.7.

<Computer systems: a programmer's perspective>

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.