<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta name="robots" content="index,nofollow">



<title>ProfilingCounts - MLton Standard ML Compiler (SML Compiler)</title>
<link rel="stylesheet" type="text/css" charset="iso-8859-1" media="all" href="common.css">
<link rel="stylesheet" type="text/css" charset="iso-8859-1" media="screen" href="screen.css">
<link rel="stylesheet" type="text/css" charset="iso-8859-1" media="print" href="print.css">


<link rel="Start" href="Home">


</head>

<body lang="en" dir="ltr">

<table bgcolor = lightblue cellspacing = 0 style = "border: 0px;" width = 100%>
  <tr>
    <td style = "
		border: 0px;
		color: darkblue; 
		font-size: 150%;
		text-align: left;">
      <a class = mltona href="Home">MLton 20051202</a>
    <td style = "
		border: 0px;
		font-size: 150%;
		text-align: center;
		width: 50%;">
      ProfilingCounts
    <td style = "
		border: 0px;
		text-align: right;">
      <table cellspacing = 0 style = "border: 0px">
        <tr style = "vertical-align: middle;">
      </table>
  <tr style = "background-color: white;">
    <td colspan = 3
	style = "
		border: 0px;
		font-size:70%;
		text-align: right;">
      <a href = "Home">Home</a>
      &nbsp;<a href = "Index">Index</a>
      &nbsp;
</table>
<div id="content" lang="en" dir="ltr">
With MLton and <tt>mlprof</tt>, you can <a href="Profiling">profile</a> your program to find out how many times each function is called and how many times each branch is taken.  To do so, compile your program with <pre>-profile 
count -profile-branch true</pre>. For example, suppose that <tt>tak.sml</tt>contains the following. <p>
<pre class=code><B><FONT COLOR="#0000FF">structure</FONT></B> Tak =
   <B><FONT COLOR="#0000FF">struct</FONT></B>
      <B><FONT COLOR="#A020F0">fun</FONT></B> tak1 (x, y, z) =
         <B><FONT COLOR="#A020F0">let</FONT></B>
            <B><FONT COLOR="#A020F0">fun</FONT></B> tak2 (x, y, z) =
               <B><FONT COLOR="#A020F0">if</FONT></B> y &gt;= x
                  <B><FONT COLOR="#A020F0">then</FONT></B> z
               <B><FONT COLOR="#A020F0">else</FONT></B>
                  tak1 (tak2 (x - <B><FONT COLOR="#5F9EA0">1</FONT></B>, y, z),
                        tak2 (y - <B><FONT COLOR="#5F9EA0">1</FONT></B>, z, x),
                        tak2 (z - <B><FONT COLOR="#5F9EA0">1</FONT></B>, x, y))
         <B><FONT COLOR="#A020F0">in</FONT></B>
            <B><FONT COLOR="#A020F0">if</FONT></B> y &gt;= x
               <B><FONT COLOR="#A020F0">then</FONT></B> z
            <B><FONT COLOR="#A020F0">else</FONT></B>
               tak1 (tak2 (x - <B><FONT COLOR="#5F9EA0">1</FONT></B>, y, z),
                     tak2 (y - <B><FONT COLOR="#5F9EA0">1</FONT></B>, z, x),
                     tak2 (z - <B><FONT COLOR="#5F9EA0">1</FONT></B>, x, y))
         <B><FONT COLOR="#A020F0">end</FONT></B>
   <B><FONT COLOR="#0000FF">end</FONT></B>

<B><FONT COLOR="#A020F0">val</FONT></B> <B><FONT COLOR="#A020F0">rec</FONT></B> f =
   <B><FONT COLOR="#A020F0">fn</FONT></B> <B><FONT COLOR="#5F9EA0">0</FONT></B> =&gt; ()
    | ~<B><FONT COLOR="#5F9EA0">1</FONT></B> =&gt; print <FONT COLOR="#BC8F8F"><B>&quot;this branch is not taken\n&quot;</FONT></B>
    | n =&gt; (Tak.tak1 (<B><FONT COLOR="#5F9EA0">18</FONT></B>, <B><FONT COLOR="#5F9EA0">12</FONT></B>, <B><FONT COLOR="#5F9EA0">6</FONT></B>) ; f (n-<B><FONT COLOR="#5F9EA0">1</FONT></B>))

<B><FONT COLOR="#A020F0">val</FONT></B> _ = f <B><FONT COLOR="#5F9EA0">5000</FONT></B>

<B><FONT COLOR="#A020F0">fun</FONT></B> uncalled () = ()
</PRE>
 
</p>
<p>
Compile with count profiling and run the program. 
</p>

<pre>% mlton -profile count -profile-branch true tak.sml
% ./tak
</pre><p>
Display the profiling data, along with raw counts and file positions. 
</p>

<pre>% mlprof -raw true -show-line true tak mlmon.out
623,610,002 ticks
            function               cur       raw     
--------------------------------- ----- -------------
Tak.tak1.tak2  tak.sml: 5         38.2% (238,530,000)
Tak.tak1.tak2.&lt;true&gt;  tak.sml: 7  27.5% (171,510,000)
Tak.tak1  tak.sml: 3              10.7%  (67,025,000)
Tak.tak1.&lt;true&gt;  tak.sml: 14      10.7%  (67,025,000)
Tak.tak1.tak2.&lt;false&gt;  tak.sml: 9 10.7%  (67,020,000)
Tak.tak1.&lt;false&gt;  tak.sml: 16      2.0%  (12,490,000)
f  tak.sml: 23                     0.0%       (5,001)
f.&lt;branch&gt;  tak.sml: 25            0.0%       (5,000)
f.&lt;branch&gt;  tak.sml: 23            0.0%           (1)
uncalled  tak.sml: 29              0.0%           (0)
f.&lt;branch&gt;  tak.sml: 24            0.0%           (0)
</pre><p>
Branches are displayed with lexical nesting followed by <tt>&lt;branch&gt;</tt> where the function name would normally be, or <tt>&lt;true&gt;</tt> or <tt>&lt;false&gt;</tt> for if-expressions.  It is best to run <tt>mlprof</tt> with <tt>-show-line&nbsp;true</tt> to help identify the branch. 
</p>
<p>
One use of <tt>-profile&nbsp;count</tt> is as a code-coverage tool, to help find code in your program that hasn't been tested.  For this reason, <tt>mlprof</tt> displays functions and branches even if they have a count of zero.  As the above output shows, the branch on line 24 was never taken and the function defined on line 29 was never called.  To see zero counts, it is best to run <tt>mlprof</tt> with <tt>-raw&nbsp;true</tt>, since some code (e.g. the branch on line 23 above) will show up with <tt>0.0%</tt> but may still have been executed and hence have a nonzero raw count. 
</p>
</div>



<p>
<hr>
Last edited on 2005-12-02 04:24:22 by <span title="ppp-71-139-183-221.dsl.snfc21.pacbell.net"><a href="StephenWeeks">StephenWeeks</a></span>.
</body></html>
