-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPU-Time.html
More file actions
117 lines (101 loc) · 4.84 KB
/
CPU-Time.html
File metadata and controls
117 lines (101 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<html lang="en">
<head>
<title>CPU Time - The GNU C Library</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="The GNU C Library">
<meta name="generator" content="makeinfo 4.9">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Processor-And-CPU-Time.html#Processor-And-CPU-Time" title="Processor And CPU Time">
<link rel="next" href="Processor-Time.html#Processor-Time" title="Processor Time">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<!--
This file documents the GNU C library.
This is Edition 0.11, last updated 2007-09-09,
of `The GNU C Library Reference Manual', for version 2.7.
Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002,
2003, 2007 Free Software Foundation, Inc.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.2 or
any later version published by the Free Software Foundation; with the
Invariant Sections being ``Free Software Needs Free Documentation''
and ``GNU Lesser General Public License'', the Front-Cover texts being
``A GNU Manual'', and with the Back-Cover Texts as in (a) below. A
copy of the license is included in the section entitled "GNU Free
Documentation License".
(a) The FSF's Back-Cover Text is: ``You are free to copy and modify
this GNU Manual. Buying copies from GNU Press supports the FSF in
developing GNU and promoting software freedom.''-->
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
pre.display { font-family:inherit }
pre.format { font-family:inherit }
pre.smalldisplay { font-family:inherit; font-size:smaller }
pre.smallformat { font-family:inherit; font-size:smaller }
pre.smallexample { font-size:smaller }
pre.smalllisp { font-size:smaller }
span.sc { font-variant:small-caps }
span.roman { font-family:serif; font-weight:normal; }
span.sansserif { font-family:sans-serif; font-weight:normal; }
--></style>
</head>
<body>
<div class="node">
<p>
<a name="CPU-Time"></a>
Next: <a rel="next" accesskey="n" href="Processor-Time.html#Processor-Time">Processor Time</a>,
Up: <a rel="up" accesskey="u" href="Processor-And-CPU-Time.html#Processor-And-CPU-Time">Processor And CPU Time</a>
<hr>
</div>
<h4 class="subsection">21.3.1 CPU Time Inquiry</h4>
<p>To get a process' CPU time, you can use the <code>clock</code> function. This
facility is declared in the header file <samp><span class="file">time.h</span></samp>.
<a name="index-time_002eh-2605"></a>
In typical usage, you call the <code>clock</code> function at the beginning
and end of the interval you want to time, subtract the values, and then
divide by <code>CLOCKS_PER_SEC</code> (the number of clock ticks per second)
to get processor time, like this:
<pre class="smallexample"> #include <time.h>
clock_t start, end;
double cpu_time_used;
start = clock();
... /* <span class="roman">Do the work.</span> */
end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
</pre>
<p>Do not use a single CPU time as an amount of time; it doesn't work that
way. Either do a subtraction as shown above or query processor time
directly. See <a href="Processor-Time.html#Processor-Time">Processor Time</a>.
<p>Different computers and operating systems vary wildly in how they keep
track of CPU time. It's common for the internal processor clock
to have a resolution somewhere between a hundredth and millionth of a
second.
<!-- time.h -->
<!-- ISO -->
<div class="defun">
— Macro: int <b>CLOCKS_PER_SEC</b><var><a name="index-CLOCKS_005fPER_005fSEC-2606"></a></var><br>
<blockquote><p>The value of this macro is the number of clock ticks per second measured
by the <code>clock</code> function. POSIX requires that this value be one
million independent of the actual resolution.
</p></blockquote></div>
<!-- time.h -->
<!-- POSIX.1 -->
<div class="defun">
— Macro: int <b>CLK_TCK</b><var><a name="index-CLK_005fTCK-2607"></a></var><br>
<blockquote><p>This is an obsolete name for <code>CLOCKS_PER_SEC</code>.
</p></blockquote></div>
<!-- time.h -->
<!-- ISO -->
<div class="defun">
— Data Type: <b>clock_t</b><var><a name="index-clock_005ft-2608"></a></var><br>
<blockquote><p>This is the type of the value returned by the <code>clock</code> function.
Values of type <code>clock_t</code> are numbers of clock ticks.
</p></blockquote></div>
<!-- time.h -->
<!-- ISO -->
<div class="defun">
— Function: clock_t <b>clock</b> (<var>void</var>)<var><a name="index-clock-2609"></a></var><br>
<blockquote><p>This function returns the calling process' current CPU time. If the CPU
time is not available or cannot be represented, <code>clock</code> returns the
value <code>(clock_t)(-1)</code>.
</p></blockquote></div>
</body></html>