Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: "Mattias Waldau" <mattias.waldau@abc.se>
To: <caml-list@inria.fr>
Subject: OCaml is fast!
Date: Wed, 22 Nov 2000 22:35:56 +0100	[thread overview]
Message-ID: <HDEEKOMJILGEIHIMAPCDOELHDKAA.mattias.waldau@abc.se> (raw)
In-Reply-To: <20001121090507.A31518@opus.davidb.org>

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




  parent reply	other threads:[~2000-11-23 12:24 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <000601bfe231$082d61d0$210148bf@dylan>
2000-11-21 17:05 ` Camlmake David Brown
2000-11-22 16:55   ` Camlmake David Brown
2000-11-22 21:35   ` Mattias Waldau [this message]
2000-11-23 13:03     ` OCaml is fast! Xavier Leroy

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=HDEEKOMJILGEIHIMAPCDOELHDKAA.mattias.waldau@abc.se \
    --to=mattias.waldau@abc.se \
    --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