<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="Asciidoctor 2.0.26">
<title>ProfilingTime</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:300,300italic,400,400italic,600,600italic%7CNoto+Serif:400,400italic,700,700italic%7CDroid+Sans+Mono:400,700">
<link rel="stylesheet" href="./asciidoctor.css">
<link rel="stylesheet" href="./mlton.css">

</head>
<body class="article">
<div id="mlton-header">
<div id="mlton-header-text">
<h2>
<a href="./Home">
MLton
20241230+git20251029+dfsg-5
</a>
</h2>
</div>
</div>
<div id="header">
<h1>ProfilingTime</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph">
<p>With MLton and <code>mlprof</code>, you can <a href="Profiling">profile</a> your program to
find out how much time is spent in each function over an entire run of
the program.  To do so, compile your program with <code>-profile time</code>.
For example, suppose that <code>tak.sml</code> contains the following.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="sml">Unresolved directive in ProfilingTime.adoc - include::https://raw.github.com/MLton/mlton/master/doc/examples/profiling/tak.sml[indent=0]</code></pre>
</div>
</div>
<div class="paragraph">
<p>Compile with time profiling and run the program.</p>
</div>
<div class="listingblock">
<div class="content">
<pre>% mlton -profile time tak.sml
% ./tak</pre>
</div>
</div>
<div class="paragraph">
<p>Display the profiling data.</p>
</div>
<div class="listingblock">
<div class="content">
<pre>% mlprof tak mlmon.out
6.00 seconds of CPU time (0.00 seconds GC)
function     cur
------------- -----
Tak.tak1.tak2 75.8%
Tak.tak1      24.2%</pre>
</div>
</div>
<div class="paragraph">
<p>This example shows how <code>mlprof</code> indicates lexical nesting: as a
sequence of period-separated names indicating the structures and
functions in which a function definition is nested.  The profiling
data shows that roughly three-quarters of the time is spent in the
<code>Tak.tak1.tak2</code> function, while the rest is spent in <code>Tak.tak1</code>.</p>
</div>
<div class="paragraph">
<p>Display raw counts in addition to percentages with <code>-raw true</code>.</p>
</div>
<div class="listingblock">
<div class="content">
<pre>% mlprof -raw true tak mlmon.out
6.00 seconds of CPU time (0.00 seconds GC)
  function     cur    raw
------------- ----- -------
Tak.tak1.tak2 75.8% (4.55s)
Tak.tak1      24.2% (1.45s)</pre>
</div>
</div>
<div class="paragraph">
<p>Display the file name and line number for each function in addition to
its name with <code>-show-line true</code>.</p>
</div>
<div class="listingblock">
<div class="content">
<pre>% mlprof -show-line true tak mlmon.out
6.00 seconds of CPU time (0.00 seconds GC)
        function           cur
------------------------- -----
Tak.tak1.tak2  tak.sml: 5 75.8%
Tak.tak1  tak.sml: 3      24.2%</pre>
</div>
</div>
<div class="paragraph">
<p>Time profiling is designed to have a very small performance impact.
However, in some cases there will be a run-time performance cost,
which may perturb the results.</p>
</div>
<div class="paragraph">
<p>You can also compile with <code>-profile time -profile-branch true</code> to find
out how much time is spent in each branch of a function; see
<a href="ProfilingCounts">ProfilingCounts</a> for more details on <code>-profile-branch</code>.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_caveats">Caveats</h2>
<div class="sectionbody">
<div class="paragraph">
<p>With <code>-profile time</code>, use of the following in your program will cause
a run-time error, since they would interfere with the profiler signal
handler.</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>MLton.Itimer.set (MLton.Itimer.Prof, &#8230;&#8203;)</code></p>
</li>
<li>
<p><code>MLton.Signal.setHandler (MLton.Signal.prof, &#8230;&#8203;)</code></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Also, because of the random sampling used to implement <code>-profile
time</code>, it is best to have a long running program (at least tens of
seconds) in order to get reasonable time</p>
</div>
</div>
</div>
</div>
</body>
</html>