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

* Re: [Caml-list] Benchmark: ref update vs argument passing
  2001-11-20 23:42 ` Marcin 'Qrczak' Kowalczyk
@ 2001-11-21  0:20   ` malc
  0 siblings, 0 replies; 5+ messages in thread
From: malc @ 2001-11-21  0:20 UTC (permalink / raw)
  To: Marcin 'Qrczak' Kowalczyk; +Cc: caml-list

On Tue, 20 Nov 2001, Marcin 'Qrczak' Kowalczyk wrote:

> Wed, 21 Nov 2001 01:35:53 +0300 (MSK), malc <malc@pulsesoft.com> pisze:
> 
> >> let use_refs' time =
> >>   let rif = ref 0 in
> >>   for i = 0 to time - 1 do rif := !rif + 1 done;
> >>   !rif
> > let use_refs' time =
> >   let rif = ref 0 in
> >   for i = 0 to pred time do incr rif done;
> >   !rif
> 
> The generated code is exactly the same :-P
Precisely ;) It's more "camlish" in my opinion, which is why i couldnt
resist posting it.

> 
> But none of the following produces optimal code (judging by looking
> at the generated code):
> 
> let current = ref 0
> let next1 () = let x = !current in incr current; x
> let next2 () = let x = !current in current := x + 1; x
> let next3 () = incr current; !current
> let next4 () = let x = !current + 1 in current := x; x
> 
> Moreover, making the counter private on the declaration level:
> 
> let next1 =
>   let current = ref 0 in
>   fun () -> let x = !current in incr current; x
> 
> yields worse code, even though the semantics is the same.
Semantics are not the same here: first 'current' is module global and
statically allocated in module storage. Second 'current' is lexically
bound.

When i was playing with Doug Bagley's "Great Computer Language Shootout"
http://www.bagley.org/~doug/shootout/bench/sumcol/ , i found that no
matter how hard i try to invent some ingenious summing function caml
generated code leaves a lot to be desired. (I think this holds for any
use of global references).

P.S. When playing with those functions you might find -dcmm option
useful.

-- 
mailto:malc@pulsesoft.com

-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* Re: [Caml-list] Benchmark: ref update vs argument passing
       [not found] <9teme6$bn2$1@qrnik.zagroda>
@ 2001-11-20 23:42 ` Marcin 'Qrczak' Kowalczyk
  2001-11-21  0:20   ` malc
  0 siblings, 1 reply; 5+ messages in thread
From: Marcin 'Qrczak' Kowalczyk @ 2001-11-20 23:42 UTC (permalink / raw)
  To: caml-list

Wed, 21 Nov 2001 01:35:53 +0300 (MSK), malc <malc@pulsesoft.com> pisze:

>> let use_refs' time =
>>   let rif = ref 0 in
>>   for i = 0 to time - 1 do rif := !rif + 1 done;
>>   !rif
> let use_refs' time =
>   let rif = ref 0 in
>   for i = 0 to pred time do incr rif done;
>   !rif

The generated code is exactly the same :-P

But none of the following produces optimal code (judging by looking
at the generated code):

let current = ref 0
let next1 () = let x = !current in incr current; x
let next2 () = let x = !current in current := x + 1; x
let next3 () = incr current; !current
let next4 () = let x = !current + 1 in current := x; x

Moreover, making the counter private on the declaration level:

let next1 =
  let current = ref 0 in
  fun () -> let x = !current in incr current; x

yields worse code, even though the semantics is the same.

-- 
 __("<  Marcin Kowalczyk * qrczak@knm.org.pl http://qrczak.ids.net.pl/
 \__/
  ^^
QRCZAK

-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* Re: [Caml-list] Benchmark: ref update vs argument passing
  2001-11-20 15:37 ` Marcin 'Qrczak' Kowalczyk
@ 2001-11-20 22:35   ` malc
  0 siblings, 0 replies; 5+ messages in thread
From: malc @ 2001-11-20 22:35 UTC (permalink / raw)
  To: caml-list

On Tue, 20 Nov 2001, Marcin 'Qrczak' Kowalczyk wrote:

> Tue, 20 Nov 2001 16:11:27 +0100, Stefano Zacchiroli <zack@cs.unibo.it> pisze:
> 
> > Are these tests realistic or I am missing something?
> 
> IMHO they are too small to draw generic conclusions.
> 
> You can compile with 'ocamlopt -S' and see the assembler output.
> This one looks yet better than use_arg (on Intel at least):
> 
> let use_refs' time =
>   let rif = ref 0 in
>   for i = 0 to time - 1 do rif := !rif + 1 done;
>   !rif
let use_refs' time =
  let rif = ref 0 in
  for i = 0 to pred time do incr rif done;
  !rif

Sorry couldnt resist..

-- 
mailto:malc@pulsesoft.com

-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* Re: [Caml-list] Benchmark: ref update vs argument passing
       [not found] <9tdsbg$qjc$1@qrnik.zagroda>
@ 2001-11-20 15:37 ` Marcin 'Qrczak' Kowalczyk
  2001-11-20 22:35   ` malc
  0 siblings, 1 reply; 5+ messages in thread
From: Marcin 'Qrczak' Kowalczyk @ 2001-11-20 15:37 UTC (permalink / raw)
  To: caml-list

Tue, 20 Nov 2001 16:11:27 +0100, Stefano Zacchiroli <zack@cs.unibo.it> pisze:

> Are these tests realistic or I am missing something?

IMHO they are too small to draw generic conclusions.

You can compile with 'ocamlopt -S' and see the assembler output.
This one looks yet better than use_arg (on Intel at least):

let use_refs' time =
  let rif = ref 0 in
  for i = 0 to time - 1 do rif := !rif + 1 done;
  !rif

-- 
 __("<  Marcin Kowalczyk * qrczak@knm.org.pl http://qrczak.ids.net.pl/
 \__/
  ^^
QRCZAK

-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

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