From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id NAA31475 for caml-red; Thu, 23 Nov 2000 13:24:34 +0100 (MET) Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id WAA30667 for ; Wed, 22 Nov 2000 22:36:05 +0100 (MET) Received: from tor.abc.se (ns.abc.se [195.17.72.11]) by nez-perce.inria.fr (8.11.1/8.10.0) with ESMTP id eAMLa4T26599 for ; Wed, 22 Nov 2000 22:36:04 +0100 (MET) Received: from gateway (dialup-54 [195.17.73.54]) by tor.abc.se (8.9.3+Sun/8.9.3) with SMTP id WAA07766 for ; Wed, 22 Nov 2000 22:35:58 +0100 (MET) From: "Mattias Waldau" To: Subject: OCaml is fast! Date: Wed, 22 Nov 2000 22:35:56 +0100 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook IMO, Build 9.0.2416 (9.0.2911.0) X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400 Importance: Normal In-Reply-To: <20001121090507.A31518@opus.davidb.org> Sender: weis@pauillac.inria.fr 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 = # Format.fprintf Format.std_formatter "@[with_oo:(x=%i; y=%i; y=%i)@]";; with_oo:(x=- : int -> int -> int -> unit = (* # let t = new with_oo;; val t : with_oo = # t;; - : with_oo = # 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