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