caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Stefano Zacchiroli <zack@cs.unibo.it>
To: Inria Ocaml Mailing List <caml-list@inria.fr>
Subject: [Caml-list] Benchmark: ref update vs argument passing
Date: Tue, 20 Nov 2001 16:11:27 +0100	[thread overview]
Message-ID: <20011120161127.A32409@cs.unibo.it> (raw)

[-- 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()


             reply	other threads:[~2001-11-20 15:12 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-11-20 15:11 Stefano Zacchiroli [this message]
     [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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20011120161127.A32409@cs.unibo.it \
    --to=zack@cs.unibo.it \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).