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

Sampling-Error.html 6.3KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- This file documents the gprof profiler of the GNU system.
  4. Copyright (C) 1988-2020 Free Software Foundation, Inc.
  5. Permission is granted to copy, distribute and/or modify this document
  6. under the terms of the GNU Free Documentation License, Version 1.3
  7. or any later version published by the Free Software Foundation;
  8. with no Invariant Sections, with no Front-Cover Texts, and with no
  9. Back-Cover Texts. A copy of the license is included in the
  10. section entitled "GNU Free Documentation License".
  11. -->
  12. <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
  13. <head>
  14. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  15. <title>Sampling Error (GNU gprof)</title>
  16. <meta name="description" content="Sampling Error (GNU gprof)">
  17. <meta name="keywords" content="Sampling Error (GNU gprof)">
  18. <meta name="resource-type" content="document">
  19. <meta name="distribution" content="global">
  20. <meta name="Generator" content="makeinfo">
  21. <link href="index.html#Top" rel="start" title="Top">
  22. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  23. <link href="Inaccuracy.html#Inaccuracy" rel="up" title="Inaccuracy">
  24. <link href="Assumptions.html#Assumptions" rel="next" title="Assumptions">
  25. <link href="Inaccuracy.html#Inaccuracy" rel="prev" title="Inaccuracy">
  26. <style type="text/css">
  27. <!--
  28. a.summary-letter {text-decoration: none}
  29. blockquote.indentedblock {margin-right: 0em}
  30. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  31. blockquote.smallquotation {font-size: smaller}
  32. div.display {margin-left: 3.2em}
  33. div.example {margin-left: 3.2em}
  34. div.lisp {margin-left: 3.2em}
  35. div.smalldisplay {margin-left: 3.2em}
  36. div.smallexample {margin-left: 3.2em}
  37. div.smalllisp {margin-left: 3.2em}
  38. kbd {font-style: oblique}
  39. pre.display {font-family: inherit}
  40. pre.format {font-family: inherit}
  41. pre.menu-comment {font-family: serif}
  42. pre.menu-preformatted {font-family: serif}
  43. pre.smalldisplay {font-family: inherit; font-size: smaller}
  44. pre.smallexample {font-size: smaller}
  45. pre.smallformat {font-family: inherit; font-size: smaller}
  46. pre.smalllisp {font-size: smaller}
  47. span.nolinebreak {white-space: nowrap}
  48. span.roman {font-family: initial; font-weight: normal}
  49. span.sansserif {font-family: sans-serif; font-weight: normal}
  50. ul.no-bullet {list-style: none}
  51. -->
  52. </style>
  53. </head>
  54. <body lang="en">
  55. <a name="Sampling-Error"></a>
  56. <div class="header">
  57. <p>
  58. Next: <a href="Assumptions.html#Assumptions" accesskey="n" rel="next">Assumptions</a>, Up: <a href="Inaccuracy.html#Inaccuracy" accesskey="u" rel="up">Inaccuracy</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>]</p>
  59. </div>
  60. <hr>
  61. <a name="Statistical-Sampling-Error"></a>
  62. <h3 class="section">6.1 Statistical Sampling Error</h3>
  63. <p>The run-time figures that <code>gprof</code> gives you are based on a sampling
  64. process, so they are subject to statistical inaccuracy. If a function runs
  65. only a small amount of time, so that on the average the sampling process
  66. ought to catch that function in the act only once, there is a pretty good
  67. chance it will actually find that function zero times, or twice.
  68. </p>
  69. <p>By contrast, the number-of-calls and basic-block figures are derived
  70. by counting, not sampling. They are completely accurate and will not
  71. vary from run to run if your program is deterministic and single
  72. threaded. In multi-threaded applications, or single threaded
  73. applications that link with multi-threaded libraries, the counts are
  74. only deterministic if the counting function is thread-safe. (Note:
  75. beware that the mcount counting function in glibc is <em>not</em>
  76. thread-safe). See <a href="Implementation.html#Implementation">Implementation of Profiling</a>.
  77. </p>
  78. <p>The <em>sampling period</em> that is printed at the beginning of the flat
  79. profile says how often samples are taken. The rule of thumb is that a
  80. run-time figure is accurate if it is considerably bigger than the sampling
  81. period.
  82. </p>
  83. <p>The actual amount of error can be predicted.
  84. For <var>n</var> samples, the <em>expected</em> error
  85. is the square-root of <var>n</var>. For example,
  86. if the sampling period is 0.01 seconds and <code>foo</code>&rsquo;s run-time is 1 second,
  87. <var>n</var> is 100 samples (1 second/0.01 seconds), sqrt(<var>n</var>) is 10 samples, so
  88. the expected error in <code>foo</code>&rsquo;s run-time is 0.1 seconds (10*0.01 seconds),
  89. or ten percent of the observed value.
  90. Again, if the sampling period is 0.01 seconds and <code>bar</code>&rsquo;s run-time is
  91. 100 seconds, <var>n</var> is 10000 samples, sqrt(<var>n</var>) is 100 samples, so
  92. the expected error in <code>bar</code>&rsquo;s run-time is 1 second,
  93. or one percent of the observed value.
  94. It is likely to
  95. vary this much <em>on the average</em> from one profiling run to the next.
  96. (<em>Sometimes</em> it will vary more.)
  97. </p>
  98. <p>This does not mean that a small run-time figure is devoid of information.
  99. If the program&rsquo;s <em>total</em> run-time is large, a small run-time for one
  100. function does tell you that that function used an insignificant fraction of
  101. the whole program&rsquo;s time. Usually this means it is not worth optimizing.
  102. </p>
  103. <p>One way to get more accuracy is to give your program more (but similar)
  104. input data so it will take longer. Another way is to combine the data from
  105. several runs, using the &lsquo;<samp>-s</samp>&rsquo; option of <code>gprof</code>. Here is how:
  106. </p>
  107. <ol>
  108. <li> Run your program once.
  109. </li><li> Issue the command &lsquo;<samp>mv gmon.out gmon.sum</samp>&rsquo;.
  110. </li><li> Run your program again, the same as before.
  111. </li><li> Merge the new data in <samp>gmon.out</samp> into <samp>gmon.sum</samp> with this command:
  112. <div class="example">
  113. <pre class="example">gprof -s <var>executable-file</var> gmon.out gmon.sum
  114. </pre></div>
  115. </li><li> Repeat the last two steps as often as you wish.
  116. </li><li> Analyze the cumulative data using this command:
  117. <div class="example">
  118. <pre class="example">gprof <var>executable-file</var> gmon.sum &gt; <var>output-file</var>
  119. </pre></div>
  120. </li></ol>
  121. <hr>
  122. <div class="header">
  123. <p>
  124. Next: <a href="Assumptions.html#Assumptions" accesskey="n" rel="next">Assumptions</a>, Up: <a href="Inaccuracy.html#Inaccuracy" accesskey="u" rel="up">Inaccuracy</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>]</p>
  125. </div>
  126. </body>
  127. </html>