<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <!-- This file documents the gprof profiler of the GNU system. Copyright (C) 1988-2021 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 no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License". --> <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ --> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Sampling Error (GNU gprof)</title> <meta name="description" content="Sampling Error (GNU gprof)"> <meta name="keywords" content="Sampling Error (GNU gprof)"> <meta name="resource-type" content="document"> <meta name="distribution" content="global"> <meta name="Generator" content="makeinfo"> <link href="index.html#Top" rel="start" title="Top"> <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents"> <link href="Inaccuracy.html#Inaccuracy" rel="up" title="Inaccuracy"> <link href="Assumptions.html#Assumptions" rel="next" title="Assumptions"> <link href="Inaccuracy.html#Inaccuracy" rel="prev" title="Inaccuracy"> <style type="text/css"> <!-- a.summary-letter {text-decoration: none} blockquote.indentedblock {margin-right: 0em} blockquote.smallindentedblock {margin-right: 0em; font-size: smaller} blockquote.smallquotation {font-size: smaller} div.display {margin-left: 3.2em} div.example {margin-left: 3.2em} div.lisp {margin-left: 3.2em} div.smalldisplay {margin-left: 3.2em} div.smallexample {margin-left: 3.2em} div.smalllisp {margin-left: 3.2em} kbd {font-style: oblique} pre.display {font-family: inherit} pre.format {font-family: inherit} pre.menu-comment {font-family: serif} pre.menu-preformatted {font-family: serif} pre.smalldisplay {font-family: inherit; font-size: smaller} pre.smallexample {font-size: smaller} pre.smallformat {font-family: inherit; font-size: smaller} pre.smalllisp {font-size: smaller} span.nolinebreak {white-space: nowrap} span.roman {font-family: initial; font-weight: normal} span.sansserif {font-family: sans-serif; font-weight: normal} ul.no-bullet {list-style: none} --> </style> </head> <body lang="en"> <a name="Sampling-Error"></a> <div class="header"> <p> Next: <a href="Assumptions.html#Assumptions" accesskey="n" rel="next">Assumptions</a>, Up: <a href="Inaccuracy.html#Inaccuracy" accesskey="u" rel="up">Inaccuracy</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>]</p> </div> <hr> <a name="Statistical-Sampling-Error"></a> <h3 class="section">6.1 Statistical Sampling Error</h3> <p>The run-time figures that <code>gprof</code> gives you are based on a sampling process, so they are subject to statistical inaccuracy. If a function runs only a small amount of time, so that on the average the sampling process ought to catch that function in the act only once, there is a pretty good chance it will actually find that function zero times, or twice. </p> <p>By contrast, the number-of-calls and basic-block figures are derived by counting, not sampling. They are completely accurate and will not vary from run to run if your program is deterministic and single threaded. In multi-threaded applications, or single threaded applications that link with multi-threaded libraries, the counts are only deterministic if the counting function is thread-safe. (Note: beware that the mcount counting function in glibc is <em>not</em> thread-safe). See <a href="Implementation.html#Implementation">Implementation of Profiling</a>. </p> <p>The <em>sampling period</em> that is printed at the beginning of the flat profile says how often samples are taken. The rule of thumb is that a run-time figure is accurate if it is considerably bigger than the sampling period. </p> <p>The actual amount of error can be predicted. For <var>n</var> samples, the <em>expected</em> error is the square-root of <var>n</var>. For example, if the sampling period is 0.01 seconds and <code>foo</code>’s run-time is 1 second, <var>n</var> is 100 samples (1 second/0.01 seconds), sqrt(<var>n</var>) is 10 samples, so the expected error in <code>foo</code>’s run-time is 0.1 seconds (10*0.01 seconds), or ten percent of the observed value. Again, if the sampling period is 0.01 seconds and <code>bar</code>’s run-time is 100 seconds, <var>n</var> is 10000 samples, sqrt(<var>n</var>) is 100 samples, so the expected error in <code>bar</code>’s run-time is 1 second, or one percent of the observed value. It is likely to vary this much <em>on the average</em> from one profiling run to the next. (<em>Sometimes</em> it will vary more.) </p> <p>This does not mean that a small run-time figure is devoid of information. If the program’s <em>total</em> run-time is large, a small run-time for one function does tell you that that function used an insignificant fraction of the whole program’s time. Usually this means it is not worth optimizing. </p> <p>One way to get more accuracy is to give your program more (but similar) input data so it will take longer. Another way is to combine the data from several runs, using the ‘<samp>-s</samp>’ option of <code>gprof</code>. Here is how: </p> <ol> <li> Run your program once. </li><li> Issue the command ‘<samp>mv gmon.out gmon.sum</samp>’. </li><li> Run your program again, the same as before. </li><li> Merge the new data in <samp>gmon.out</samp> into <samp>gmon.sum</samp> with this command: <div class="example"> <pre class="example">gprof -s <var>executable-file</var> gmon.out gmon.sum </pre></div> </li><li> Repeat the last two steps as often as you wish. </li><li> Analyze the cumulative data using this command: <div class="example"> <pre class="example">gprof <var>executable-file</var> gmon.sum > <var>output-file</var> </pre></div> </li></ol> <hr> <div class="header"> <p> Next: <a href="Assumptions.html#Assumptions" accesskey="n" rel="next">Assumptions</a>, Up: <a href="Inaccuracy.html#Inaccuracy" accesskey="u" rel="up">Inaccuracy</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>]</p> </div> </body> </html>