Question
How to measure performances of code?
Answer
One has two possibilities to measure performances of ocaml code:
- byte code profiling
- native code profiling
If you are looking for rough times you have to go for native code profiling. With native code profiling you indeed get information about how much time your program sits in each functions, including low level functions like ones invoked by the ocaml garbage collector.
OTOH with byte code profiling you will get "commented" ocaml sources.
You can get a copy of your source code filled with information that tell you how many times the execution reached a given point in the code.
The better approach to profiling is use both of them :-) Once you know where your program sits much time _and_ which are the more used parts of your program ... then you know _where_ to optimize.
BYTE CODE PROFILING
In order to perform bytecode profiling you need to compile your sources using ocamlcp (or "ocamlfind ocamlcp" if you are using findlib) instead of ocamlc. [ To avoid common pitfalls I suggest you to provide a .mli
interface for each .ml source file when profiling. This is because .cmi produced by ocamlcp are not compatibile with those produced by ocamlc ]
Then you have simply to run the obtained executable. This executable should terminate with an exit statemente and should not end with an uncaught exception otherwise profiling information wont be collected.
Profiling information will be saved to ocamlprof.dump during the execution of your program. Note that this file isn’t overwritten each time you execute your program, but profiling information are cumulated here. To avoid this you should delete the file after each execution ...
To get "commented" sources now you should simply run
ocamlprof <sourcefile>
the resulting source is printed to stdout. It is a copy of sourcefile filled with comments including integer values. Each value represent how many time the execution flow reached that point.
By default comments are added inside the body of each function (representing how many time that function has been invoked) and near match .. with constructs (representing how many time the pattern match has been used). You can get comments also elsewere (if .. then .. else, while, for ...) using the "-p" switch of ocamlcp.
NATIVE CODE PROFILING
Native code profiling is performed in the same way it’s done with standard gcc tools. You just have to add "-p" switch when _linking_ to ocamlopt (or ocamlfind ocamlopt, though). [ Remember that time profiling is available only for native code executable ]
Then you can simply run your program as before. Running it will create a gmon.out file in the current directory. This file can be use with standard GNU gprof. Just run
gprof <your_program>
after having executed it.
This will output a big table. The most interesting part is usually the initial one which show how many time (both absolutely and in percentage) has been used inside each functions of your program.
Please note that function names are not ocaml function names but C ones.
To recognize your stuff remember that each functions
Module.name
has a corresponding C name like
Module__name_<seqno>
the seqno is used as an identifier. If you have many function names which differ only for the identifier is because they are anonimous ones like "fun x -> x * 2".
© Stefano ’’Zack’’ Zacchiroli GNU FDL 2003