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