caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Profiling a function execution
@ 2003-11-13 22:34 Daniel Bünzli
  2003-11-13 23:53 ` Brian Hurt
  2003-11-25 18:05 ` Xavier Leroy
  0 siblings, 2 replies; 11+ messages in thread
From: Daniel Bünzli @ 2003-11-13 22:34 UTC (permalink / raw)
  To: caml-list

Hello,

To compare different implementations of a function I would like to 
profile its execution in time and heap memory usage (at least orders of 
magnitude). To do so, I use the code below, relying on the Gc and Unix 
libraries.

I have the following questions :

1) When I start profiling should I prefer a Gc.compact to Gc.full_major 
?

2) Unix.times seems to have a low resolution, which means that my 
timings are often 0.0 (unless I execute the function a lot of times). I 
don't want to use Unix.gettimeofday because this prevents me to make 
the difference between user and system time. Has anybody bindings to 
the getrusage function or another idea ?

3) What is the accuracy of these results ? E.g. I read in the 
documentation of the Gc module that the field minor_words is only an 
approximation in programs compiled to native code. Is it also true for 
the other fields ? How much can I trust the figures I get ?

4) Is it possible to know at runtime whether we are running native code 
or interpreted bytecode ?

Thanks for your answers,

Daniel


-- profile.ml --

type t = { minor_bytes : float;
	   promoted_bytes : float;
	   major_bytes : float;
	   allocated_bytes : float;
	
	   minor_collections : float;
	   major_collections : float;
	
	   user_time : float;
	   system_time : float }

(* Bytes per words *)
let bpw = float_of_int (Sys.word_size / 8)

(* Heap allocation overhead due to profiling *)
let heap_overhead =
   let s = Gc.stat() in
   ignore(Unix.times());
   ignore(Unix.times());
   let s' = Gc.stat() in
   ((s'.Gc.minor_words +. s'.Gc.major_words -. s'.Gc.promoted_words) -.
   (s.Gc.minor_words +. s.Gc.major_words -. s.Gc.promoted_words)) *. bpw

let execution_mean (f : 'a -> 'b) (arg : 'a) n =
   Gc.full_major ();
   let s = Gc.stat () in
   let t = Unix.times () in
   for i = 1 to n do
     ignore(f arg)
   done;
   let t' = Unix.times () in
   let s' = Gc.stat () in
   let mi, pro, ma =
     ((s'.Gc.minor_words -. s.Gc.minor_words) *. bpw) -. heap_overhead,
     (s'.Gc.promoted_words -. s.Gc.promoted_words) *. bpw,
     (s'.Gc.major_words -. s.Gc.major_words) *. bpw in
   let n' = float_of_int n in
   { minor_bytes = mi /. n';
     promoted_bytes = pro /. n';
     major_bytes = ma /. n';
     allocated_bytes = (mi +. ma -. pro) /. n';

     minor_collections =
       (float_of_int (s'.Gc.minor_collections - s.Gc.minor_collections)) 
/. n';
     major_collections =
       (float_of_int (s'.Gc.major_collections - s.Gc.major_collections)) 
/. n';

     user_time = (t'.Unix.tms_utime -. t.Unix.tms_utime) /. n';
     system_time = (t'.Unix.tms_stime -. t.Unix.tms_stime) /. n'
   }

let execution (f : 'a -> 'b) (arg : 'a) = execution_mean f arg 10_000

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2003-11-26 12:23 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-11-13 22:34 [Caml-list] Profiling a function execution Daniel Bünzli
2003-11-13 23:53 ` Brian Hurt
2003-11-14 12:23   ` Ville-Pertti Keinonen
2003-11-14 12:33   ` Richard Jones
2003-11-15 11:11     ` Wolfgang Lux
2003-11-15 12:21       ` David MENTRE
2003-11-15 12:54       ` Richard Jones
2003-11-25 18:05 ` Xavier Leroy
2003-11-25 21:54   ` Daniel Bünzli
2003-11-25 22:38   ` Kim Nguyen
2003-11-26 12:23   ` Ville-Pertti Keinonen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).