caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Execution time of class versus record
@ 2007-06-24 15:20 tmp123
  0 siblings, 0 replies; 5+ messages in thread
From: tmp123 @ 2007-06-24 15:20 UTC (permalink / raw)
  To: caml-list

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.






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

* Re: Execution time of class versus record
  2007-06-24 15:14 tmp123
@ 2007-06-26 13:35 ` Sam Steingold
  0 siblings, 0 replies; 5+ messages in thread
From: Sam Steingold @ 2007-06-26 13:35 UTC (permalink / raw)
  To: tmp123; +Cc: caml-list

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

tmp123@menta.net wrote:
> 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).

As other have explained, classes use hash table lookup per slot access,
while records access fields by offsets computed at compile-time.

> Please, knows someone what I'm doing wrong?
>  let t0 = Unix.gettimeofday () in
>  Printf.printf "elapsed = %f\n" (Unix.gettimeofday() -. t0)

Unix.gettimeofday is "wall clock".
Unix.times is "CPU time".
use the latter to time your programs because the former depends on the
machine load at the time of the test while the latter does not.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGgRYIPp1Qsf2qnMcRAiC5AJkBPG/XrrH3mSZuP+YFTtLk+xF4YACdGmdF
zijSDKnxkm6BIpXDQ+DkLwE=
=lchS
-----END PGP SIGNATURE-----


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

* Re: Execution time of class versus record
       [not found] <20070626065522.6175FBC77@yquem.inria.fr>
@ 2007-06-26  7:36 ` David Allsopp
  0 siblings, 0 replies; 5+ messages in thread
From: David Allsopp @ 2007-06-26  7:36 UTC (permalink / raw)
  To: caml-list

On Jun 26, 2007, at 08:53 AM, Loup Vaillant wrote:
> > For the same reason, there is a difference between:
> >
> >   type t = A of int * int
> >
> > and:
> >
> >   type t = A of (int * int)
>
> Err, I don't get it : I see exactly the same thing (written twice) here.
>
> Are you telling that :
> type t = A of int * int   <==>   type t = (A of int) * int
>
>?

The type declarations are different - the top-level reports a different type
in each case!

The brackets matter because they tell O'Caml how many *arguments* the
constructor A will take and so affect the internal representation.

With the first type declaration, A(1, 2) is a 2-argument constructor and the
value will be represented internally as a 2-field block with tag 0 with
field 0 containing 1 and field 1 containing 2. This means that this code
wouldn't work:

    let foo = (1, 2)
    in
      A foo

but with the second type declaration it would.

With the second type declaration, A (1, 2) is a 1-argument constructor and
will be represented internally as a 1-field block with tag 0 with field 0
"pointing" to another 2-field block containing the 1 and 2.

Section #18.3.4 of the manual explains the internal representation
difference (esp. with ref to #18.3.6 where it is compared to Polymorphic
Variants) but I was sure that there was an explicit reference in the manual
to the difference between the type declarations but I can't find it now :(

The confusion arises because the notation for arguments of a constructor and
tuple construction look the same - I believe the "revised" syntax for O'Caml
changes the way these are declared to make it clearer but I've never looked
at it...


David


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

* Execution time of class versus record
@ 2007-06-24 15:18 tmp123
  0 siblings, 0 replies; 5+ messages in thread
From: tmp123 @ 2007-06-24 15:18 UTC (permalink / raw)
  To: caml-list

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.






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

* Execution time of class versus record
@ 2007-06-24 15:14 tmp123
  2007-06-26 13:35 ` Sam Steingold
  0 siblings, 1 reply; 5+ messages in thread
From: tmp123 @ 2007-06-24 15:14 UTC (permalink / raw)
  To: caml-list

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.





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

end of thread, other threads:[~2007-06-26 13:35 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-06-24 15:20 Execution time of class versus record tmp123
     [not found] <20070626065522.6175FBC77@yquem.inria.fr>
2007-06-26  7:36 ` David Allsopp
  -- strict thread matches above, loose matches on Subject: below --
2007-06-24 15:18 tmp123
2007-06-24 15:14 tmp123
2007-06-26 13:35 ` Sam Steingold

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