<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2003/11/34f26b24813463c04244b74a889dc2a3"
  from="Daniel_Bünzli &lt;daniel.buenzli@e...&gt;"
  author="Daniel_Bünzli"
  date="2003-11-13T22:34:14"
  subject="[Caml-list] Profiling a function execution"
  prev="2003/11/beb28293553e92702ce6aef53c449423"
  next="2003/11/9f922a27f598ae82ac0f62db2c953605"
  next-in-thread="2003/11/9f922a27f598ae82ac0f62db2c953605"
  prev-thread="2003/11/3f569e7c0ae6343b3f1d7e892dcba645"
  next-thread="2003/11/659715962834b450cc1e0aeddeb9dc7e"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="[Caml-list] Profiling a function execution">
<msg 
  url="2003/11/34f26b24813463c04244b74a889dc2a3"
  from="Daniel_Bünzli &lt;daniel.buenzli@e...&gt;"
  author="Daniel_Bünzli"
  date="2003-11-13T22:34:14"
  subject="[Caml-list] Profiling a function execution">
<msg 
  url="2003/11/9f922a27f598ae82ac0f62db2c953605"
  from="Brian Hurt &lt;bhurt@s...&gt;"
  author="Brian Hurt"
  date="2003-11-13T22:53:58"
  subject="Re: [Caml-list] Profiling a function execution">
<msg 
  url="2003/11/ccaf83034bb4211c2c282eaf9cc29131"
  from="Ville-Pertti Keinonen &lt;will@e...&gt;"
  author="Ville-Pertti Keinonen"
  date="2003-11-14T12:23:53"
  subject="Re: [Caml-list] Profiling a function execution">
</msg>
<msg 
  url="2003/11/188916524dd342aefc88d89218040ae7"
  from="Richard Jones &lt;rich@a...&gt;"
  author="Richard Jones"
  date="2003-11-14T12:34:04"
  subject="Re: [Caml-list] Profiling a function execution">
<msg 
  url="2003/11/517bc908d9c80ef3b698d7c821a8317a"
  from="Wolfgang Lux &lt;wlux@u...&gt;"
  author="Wolfgang Lux"
  date="2003-11-15T11:11:19"
  subject="Re: [Caml-list] Profiling a function execution">
<msg 
  url="2003/11/f561faa2124a980042c1f6b2ea4c38a4"
  from="David MENTRE &lt;dmentre@l...&gt;"
  author="David MENTRE"
  date="2003-11-15T12:21:34"
  subject="Re: [Caml-list] Profiling a function execution">
</msg>
<msg 
  url="2003/11/4ad33fa3b94199e611c2eaf3954d0041"
  from="Richard Jones &lt;rich@a...&gt;"
  author="Richard Jones"
  date="2003-11-15T12:54:11"
  subject="Re: [Caml-list] Profiling a function execution">
</msg>
</msg>
</msg>
</msg>
<msg 
  url="2003/11/6f41f08fba60b932f48b1673055f188d"
  from="Xavier Leroy &lt;xavier.leroy@i...&gt;"
  author="Xavier Leroy"
  date="2003-11-25T18:05:56"
  subject="Re: [Caml-list] Profiling a function execution">
<msg 
  url="2003/11/3926d9185cb59338ebbe489e8e1ae367"
  from="Daniel_Bünzli &lt;daniel.buenzli@e...&gt;"
  author="Daniel_Bünzli"
  date="2003-11-25T21:54:42"
  subject="Re: [Caml-list] Profiling a function execution">
</msg>
<msg 
  url="2003/11/099ebe67b9b5a5241762a30b9a733ffb"
  from="Kim Nguyen &lt;nguyen@b...&gt;"
  author="Kim Nguyen"
  date="2003-11-25T22:36:27"
  subject="Re: [Caml-list] Profiling a function execution">
</msg>
<msg 
  url="2003/11/615473df5cbf28e2eb2ecd0c2052564a"
  from="Ville-Pertti Keinonen &lt;will@e...&gt;"
  author="Ville-Pertti Keinonen"
  date="2003-11-26T12:23:29"
  subject="Re: [Caml-list] Profiling a function execution">
</msg>
</msg>
</msg>
</thread>

<contents>
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 -&gt; '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 -&gt; '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

</contents>

</message>

