Version française
Home     About     Download     Resources     Contact us    
Browse thread
Execution time of class versus record
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: tmp123@m...
Subject: Execution time of class versus record
Hello,

I've tried to implement two equivalent small programs, the one using
class, the other one using records. The resulting execution times says
that class are 7-8 times slower than record (compiled with ocamlopt in a
Intel machine).

Please, knows someone what I'm doing wrong?

The programs are:

records1.ml
=========
type ra = { mutable t : int }

let main () =
  let a = { t = 0 } in
  for i = 0 to 100000 do
    a.t <- a.t + i
  done;
  Printf.printf "t = %d\n" a.t

let _ =
  let t0 = Unix.gettimeofday () in
  main();
  Printf.printf "elapsed = %f\n" (Unix.gettimeofday() -. t0)

class1.ml
=======
class ca =
  object
    val mutable t = 0

    method add x = t <- t+x
    method get () = t
  end

let main () =
  let a = new ca in
  for i = 0 to 100000 do
    a#add i
  done;
  Printf.printf "t = %d\n" (a#get())

let _ =
  let t0 = Unix.gettimeofday () in
  main();
  Printf.printf "elapsed = %f\n" (Unix.gettimeofday() -. t0)

Thanks a lot.