| 1 |
= ProfilingTime |
| 2 |
|
| 3 |
With MLton and `mlprof`, you can <<Profiling#,profile>> your program to |
| 4 |
find out how much time is spent in each function over an entire run of |
| 5 |
the program. To do so, compile your program with `-profile time`. |
| 6 |
For example, suppose that `tak.sml` contains the following. |
| 7 |
|
| 8 |
[source,sml] |
| 9 |
---- |
| 10 |
include::https://raw.github.com/MLton/mlton/master/doc/examples/profiling/tak.sml[indent=0] |
| 11 |
---- |
| 12 |
|
| 13 |
Compile with time profiling and run the program. |
| 14 |
---- |
| 15 |
% mlton -profile time tak.sml |
| 16 |
% ./tak |
| 17 |
---- |
| 18 |
|
| 19 |
Display the profiling data. |
| 20 |
---- |
| 21 |
% mlprof tak mlmon.out |
| 22 |
6.00 seconds of CPU time (0.00 seconds GC) |
| 23 |
function cur |
| 24 |
------------- ----- |
| 25 |
Tak.tak1.tak2 75.8% |
| 26 |
Tak.tak1 24.2% |
| 27 |
---- |
| 28 |
|
| 29 |
This example shows how `mlprof` indicates lexical nesting: as a |
| 30 |
sequence of period-separated names indicating the structures and |
| 31 |
functions in which a function definition is nested. The profiling |
| 32 |
data shows that roughly three-quarters of the time is spent in the |
| 33 |
`Tak.tak1.tak2` function, while the rest is spent in `Tak.tak1`. |
| 34 |
|
| 35 |
Display raw counts in addition to percentages with `-raw true`. |
| 36 |
---- |
| 37 |
% mlprof -raw true tak mlmon.out |
| 38 |
6.00 seconds of CPU time (0.00 seconds GC) |
| 39 |
function cur raw |
| 40 |
------------- ----- ------- |
| 41 |
Tak.tak1.tak2 75.8% (4.55s) |
| 42 |
Tak.tak1 24.2% (1.45s) |
| 43 |
---- |
| 44 |
|
| 45 |
Display the file name and line number for each function in addition to |
| 46 |
its name with `-show-line true`. |
| 47 |
---- |
| 48 |
% mlprof -show-line true tak mlmon.out |
| 49 |
6.00 seconds of CPU time (0.00 seconds GC) |
| 50 |
function cur |
| 51 |
------------------------- ----- |
| 52 |
Tak.tak1.tak2 tak.sml: 5 75.8% |
| 53 |
Tak.tak1 tak.sml: 3 24.2% |
| 54 |
---- |
| 55 |
|
| 56 |
Time profiling is designed to have a very small performance impact. |
| 57 |
However, in some cases there will be a run-time performance cost, |
| 58 |
which may perturb the results. |
| 59 |
|
| 60 |
You can also compile with `-profile time -profile-branch true` to find |
| 61 |
out how much time is spent in each branch of a function; see |
| 62 |
<<ProfilingCounts#>> for more details on `-profile-branch`. |
| 63 |
|
| 64 |
|
| 65 |
== Caveats |
| 66 |
|
| 67 |
With `-profile time`, use of the following in your program will cause |
| 68 |
a run-time error, since they would interfere with the profiler signal |
| 69 |
handler. |
| 70 |
|
| 71 |
* `MLton.Itimer.set (MLton.Itimer.Prof, ...)` |
| 72 |
* `MLton.Signal.setHandler (MLton.Signal.prof, ...)` |
| 73 |
|
| 74 |
Also, because of the random sampling used to implement `-profile |
| 75 |
time`, it is best to have a long running program (at least tens of |
| 76 |
seconds) in order to get reasonable time |