caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Benchmark: ref update vs argument passing
@ 2001-11-20 15:11 Stefano Zacchiroli
  0 siblings, 0 replies; 5+ messages in thread
From: Stefano Zacchiroli @ 2001-11-20 15:11 UTC (permalink / raw)
  To: Inria Ocaml Mailing List

[-- Attachment #1: Type: text/plain, Size: 1170 bytes --]

I'm modelling a small simulator, the simulator is implemented with a
state machine that handle the event queue and with a scheduler that
schedule new events.
Another feature that the simulator have to handle is the accounting of
various times.

At the moment the times are kept away from the state of the state
machine in a few global references that are updated when needed, because
I thought that ref update is faster than passing these time across
functions.

I've written a small test that benchmark performance of passing argument
between funcions vs. keep them in global reference (code attached).
Surprisingly passing argument is really faster than update them through
references, look at this:

$ time ./a.out 1000000000 ref
1000000001

real  0m24.382s
user  0m12.000s
sys 0m0.000s

$ time ./a.out 1000000000 arg
1000000001

real  0m5.317s
user  0m2.660s
sys 0m0.000s

Are these tests realistic or I am missing something?

Cheers!

-- 
Stefano "Zack" Zacchiroli <zack@cs.unibo.it> ICQ# 33538863
Home Page: http://www.cs.unibo.it/~zacchiro
Undergraduate student of Computer Science @ University of Bologna, Italy
                 - Information wants to be Open -

[-- Attachment #2: ref_vs_passing.ml --]
[-- Type: text/plain, Size: 578 bytes --]

(* check wether reference update is slower of faster than passing parameter
between functions *)

let rif = ref 0 in

let endtime = int_of_string Sys.argv.(1) in
let mode = Sys.argv.(2) in

let rec use_refs time =
  rif := !rif + 1;
  if time>0 then use_refs (time-1)
in

let rec use_arg time arg =
  if time>=0 then use_arg (time-1) (arg+1)
  else arg
in

let main_ref () = use_refs endtime in
let main_arg () = use_arg endtime 0 in

let main () =
  match mode with
  | "ref" -> main_ref (); !rif
  | "arg" -> main_arg ()
  | _ -> ~-1
in

print_int (main ()); print_newline()


^ permalink raw reply	[flat|nested] 5+ messages in thread
[parent not found: <9tdsbg$qjc$1@qrnik.zagroda>]
[parent not found: <9teme6$bn2$1@qrnik.zagroda>]

end of thread, other threads:[~2001-11-21  0:22 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-11-20 15:11 [Caml-list] Benchmark: ref update vs argument passing Stefano Zacchiroli
     [not found] <9tdsbg$qjc$1@qrnik.zagroda>
2001-11-20 15:37 ` Marcin 'Qrczak' Kowalczyk
2001-11-20 22:35   ` malc
     [not found] <9teme6$bn2$1@qrnik.zagroda>
2001-11-20 23:42 ` Marcin 'Qrczak' Kowalczyk
2001-11-21  0:20   ` malc

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).