You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
5.1 KiB
HTML

<html lang="en">
<head>
<title>Sampling Error - GNU gprof</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="GNU gprof">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Inaccuracy.html#Inaccuracy" title="Inaccuracy">
<link rel="next" href="Assumptions.html#Assumptions" title="Assumptions">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<!--
This file documents the gprof profiler of the GNU system.
Copyright (C) 1988-2015 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''.
-->
<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">
<a name="Sampling-Error"></a>
<p>
Next:&nbsp;<a rel="next" accesskey="n" href="Assumptions.html#Assumptions">Assumptions</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Inaccuracy.html#Inaccuracy">Inaccuracy</a>
<hr>
</div>
<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>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>The <dfn>sampling period</dfn> 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>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>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>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 &lsquo;<samp><span class="samp">-s</span></samp>&rsquo; option of <code>gprof</code>. Here is how:
<ol type=1 start=1>
<li>Run your program once.
<li>Issue the command &lsquo;<samp><span class="samp">mv gmon.out gmon.sum</span></samp>&rsquo;.
<li>Run your program again, the same as before.
<li>Merge the new data in <samp><span class="file">gmon.out</span></samp> into <samp><span class="file">gmon.sum</span></samp> with this command:
<pre class="example"> gprof -s <var>executable-file</var> gmon.out gmon.sum
</pre>
<li>Repeat the last two steps as often as you wish.
<li>Analyze the cumulative data using this command:
<pre class="example"> gprof <var>executable-file</var> gmon.sum &gt; <var>output-file</var>
</pre>
</ol>
</body></html>