17.4 Time profiling

Profiling with ocamlprof only records execution counts, not the actual time spent into each function. There is currently no way to perform time profiling on bytecode programs generated by ocamlc.

Native-code programs generated by ocamlopt can be profiled for time and execution counts using the -p option and the standard Unix profiler gprof. Just add the -p option when compiling and linking the program:

 ocamlopt -o myprog -p other-options files
 ./myprog
 gprof myprog
    

Caml function names in the output of gprof have the following format:

 Module-name_function-name_unique-number
    

Other functions shown are either parts of the Caml run-time system or external C functions linked with the program.

The output of gprof is described in the Unix manual page for gprof(1). It generally consists of two parts: a "flat" profile showing the time spent in each function and the number of invocation of each function, and a "hierarchical" profile based on the call graph. Currently, only the Intel x86/Linux and Alpha/Digital Unix ports of ocamlopt support the two profiles. On other platforms, gprof will report only the "flat" profile with just time information. When reading the output of gprof, keep in mind that the accumulated times computed by gprof are based on heuristics and may not be exact.