* OCaml is fast!
2000-11-21 17:05 ` Camlmake David Brown
2000-11-22 16:55 ` Camlmake David Brown
@ 2000-11-22 21:35 ` Mattias Waldau
2000-11-23 13:03 ` Xavier Leroy
1 sibling, 1 reply; 4+ messages in thread
From: Mattias Waldau @ 2000-11-22 21:35 UTC (permalink / raw)
To: caml-list
or is my benchmark wrong? The speed is incredable, I have a 333 Mhz PII, and
Ocaml make 50 miljon round trips in the loop per second, that is 6 cycles
per loop, and in each loop one function call + add + store is done.
Or does the compiler optimize away a lot of my code? The code can be found
in the end of the file.
I made the benchmark to compare object creation + method call versus term
creation + function call. Object creation is 10 times slower, and method
calls 5 times slower. However, this difference will not be noted in a real
program unless you use objects for the smallest and most primitive objects,
like nodes in a tree and similar.
As always, I am impressed by the speed of Ocaml. However, a small superflous
';' was difficult to find, since the compiler always complained about syntax
error on the last line in the file.
----
Mattias Waldau, CTO
Tacton AB, Saltmatargatan 7, 11359 Stockholm
Mobile +46 70 549 11 12
http://www.tacton.com mailto:mattias.waldau@tacton.com
P.s. I got a tip on how to let the top level print the contents of objects -
something I complained about earlier - just define a print-method and use
#install_printer, se below
method print ff =
Format.fprintf ff "@[with_oo:(x=%i; y=%i)@]" x y
and install it by
# let object_printer x = x #print Format.std_formatter;;
# #install_printer object_printer
(* benchmark ---------------------------------------- *)
(*
ocamlopt.exe unix.cmxa oo_test.ml
100 000 000 loops,
2 miljons object creations + method call per second
10 miljons method calls per second
25 miljons term creations + method call per second
50 miljons function calls per second
oo with create: Run time = 51
method call only: Run time = 9
create term and call function: Run time = 4
call function: Run time = 2
*)
class with_oo =
object
val mutable x = 0
val mutable y = 0
method get_x = x
method move d = x <- x + d; y <- y + d
method print ff =
Format.fprintf ff "@[with_oo:(x=%i; y=%i)@]" x y
end
(*
let object_printer x = x #print Format.std_formatter;;
#install_printer object_printer
*)
let test_oo_speed () =
for ii = 1 to 100000000 do
(new with_oo)#move ii;
done
let test_oo_speed_2 () =
let obj = new with_oo in
for ii = 1 to 100000000 do
obj#move ii;
done
(*
Format.fprintf gor verkligen magiska saker, skapar functioner
som tar olika manga argument beroende pa strangen. Men det ar
kanske inte sa svart, eftersom de ar curried. Borde testa!!
# Format.fprintf Format.std_formatter "@[with_oo:(x=%i; y=%i)@]";;
with_oo:(x=- : int -> int -> unit = <fun>
# Format.fprintf Format.std_formatter "@[with_oo:(x=%i; y=%i; y=%i)@]";;
with_oo:(x=- : int -> int -> int -> unit = <fun>
(*
# let t = new with_oo;;
val t : with_oo = <obj>
# t;;
- : with_oo = <obj>
# t#get_x;;
- : int = 0
# t#move 10;;
- : unit = ()
# t#get_x;;
- : int = 10
#
*)
*)
type nooo = {
mutable x:int;
mutable y:int;
}
let nooo_init () =
{ x=0; y=0 }
let nooo_get_x this = this.x
let nooo_move this d =
this.x <- this.x + d;
this.y <- this.y + d
let test_nooo_speed () =
for ii = 1 to 100000000 do
let term = nooo_init () in
nooo_move term ii
done
let test_nooo_speed_2 () =
let term = nooo_init () in
for ii = 1 to 100000000 do
nooo_move term ii
done
let profile_function text f =
let start_time = Unix.time () in
begin
f ();
print_newline ();
print_string text;
print_string ": Run time = ";
print_float ((Unix.time ()) -. start_time);
end
let _ = profile_function "oo with create" test_oo_speed
let _ = profile_function "method call only" test_oo_speed_2
let _ = profile_function "create term and call function" test_nooo_speed
let _ = profile_function "call function" test_nooo_speed_2
^ permalink raw reply [flat|nested] 4+ messages in thread