| <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.13"> |
| <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.12, last updated 2007-10-27, |
| of `The GNU C Library Reference Manual', for version |
| 2.8 (Sourcery G++ Lite 2011.03-41). |
| |
| Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002, |
| 2003, 2007, 2008, 2010 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.3 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 have the freedom to |
| copy and modify this GNU manual. Buying copies from the FSF |
| supports it 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> |
| <link rel="stylesheet" type="text/css" href="../cs.css"> |
| </head> |
| <body> |
| <div class="node"> |
| <a name="CPU-Time"></a> |
| <p> |
| 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-2606"></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-2607"></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-2608"></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-2609"></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-2610"></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> |
| |