Import Upstream version 20180207
[hcoop/debian/mlton.git] / doc / guide / src / ProfilingCounts.adoc
CommitLineData
7f918cf1
CE
1ProfilingCounts
2===============
3
4With MLton and `mlprof`, you can <:Profiling:profile> your program to
5find out how many times each function is called and how many times
6each branch is taken. To do so, compile your program with
7`-profile count -profile-branch true`. For example, suppose that
8`tak.sml` contains the following.
9
10[source,sml]
11----
12sys::[./bin/InclGitFile.py mlton master doc/examples/profiling/tak.sml]
13----
14
15Compile with count profiling and run the program.
16----
17% mlton -profile count -profile-branch true tak.sml
18% ./tak
19----
20
21Display the profiling data, along with raw counts and file positions.
22----
23% mlprof -raw true -show-line true tak mlmon.out
24623,610,002 ticks
25 function cur raw
26--------------------------------- ----- -------------
27Tak.tak1.tak2 tak.sml: 5 38.2% (238,530,000)
28Tak.tak1.tak2.<true> tak.sml: 7 27.5% (171,510,000)
29Tak.tak1 tak.sml: 3 10.7% (67,025,000)
30Tak.tak1.<true> tak.sml: 14 10.7% (67,025,000)
31Tak.tak1.tak2.<false> tak.sml: 9 10.7% (67,020,000)
32Tak.tak1.<false> tak.sml: 16 2.0% (12,490,000)
33f tak.sml: 23 0.0% (5,001)
34f.<branch> tak.sml: 25 0.0% (5,000)
35f.<branch> tak.sml: 23 0.0% (1)
36uncalled tak.sml: 29 0.0% (0)
37f.<branch> tak.sml: 24 0.0% (0)
38----
39
40Branches are displayed with lexical nesting followed by `<branch>`
41where the function name would normally be, or `<true>` or `<false>`
42for if-expressions. It is best to run `mlprof` with `-show-line true`
43to help identify the branch.
44
45One use of `-profile count` is as a code-coverage tool, to help find
46code in your program that hasn't been tested. For this reason,
47`mlprof` displays functions and branches even if they have a count of
48zero. As the above output shows, the branch on line 24 was never
49taken and the function defined on line 29 was never called. To see
50zero counts, it is best to run `mlprof` with `-raw true`, since some
51code (e.g. the branch on line 23 above) will show up with `0.0%` but
52may still have been executed and hence have a nonzero raw count.