caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Objects poor performance
@ 2002-11-19 18:38 Cezary Kaliszyk
  2002-11-20 11:50 ` Michal Moskal
  2002-11-25 10:33 ` Xavier Leroy
  0 siblings, 2 replies; 3+ messages in thread
From: Cezary Kaliszyk @ 2002-11-19 18:38 UTC (permalink / raw)
  To: caml-list


[-- Attachment #1.1: Type: text/plain, Size: 544 bytes --]

I tried to implement a simple double-linked list with constant time insertion
and removal. I tried just iterating over such a list with both object and
structure implementation. And these are the effects:

             Time:
Structures:  2.503s
Objects:    27.027s

The implementations are the same so why are objects that slow? I include
the tests if someone wishes to test oneself or see if anything can be
done faster.

PS. The size of stripped executable is also 2*larger with objects.
But that's not such a big problem.
-- 

[-- Attachment #1.2: object.ml --]
[-- Type: text/plain, Size: 940 bytes --]

class ['a] lst data = 
  object
    val mutable data = (data : 'a)
    val mutable prev = ((Obj.magic ()) : 'a lst)
    val mutable next = ((Obj.magic ()) : 'a lst)
    method add new_data = 
      new lst new_data in
      new_elem#set_next next;
      new_elem#set_prev (next#get_prev);
      next#set_prev new_elem;
      next <- new_elem
    method get_prev = prev
    method get_next = next
    method set_prev elem = prev <- elem
    method set_next elem = next <- elem
    method get = data
  end
;;
 
let l = new lst (0, 0);;
l#set_next l;;
l#set_prev l;;


let rec adder cnt =
  l#add (cnt, 0);
  if cnt > 0 then adder (cnt - 1)
in adder 1000;;

let rec iterator a cnt alst =
  if cnt = 0 then a else
  let (i, _) = alst#get in
  iterator (a + i) (cnt - 1) alst#get_next
;;
    
let rec test cnt =
  ignore (iterator 0 1000 l);
  if cnt = 0 then () else test (cnt - 1)
;;
  
test 1000000;;

[-- Attachment #1.3: struct.ml --]
[-- Type: text/plain, Size: 826 bytes --]

type 'a ts = {
    mutable data : 'a;
    mutable prev : 'a ts;
    mutable next : 'a ts;
  };;
      
let rec single elem =
  let ret = {
    data = elem; 
    next = Obj.magic 0;
    prev = Obj.magic 0;
  } in
  ret.next <- ret;
  ret.prev <- ret;
  ret
;;

let rec add elem ({next = n} as lst) =
  let newnext = {
    data = elem;
    prev = lst;
    next = n
  } in
  n.prev <- newnext;
  lst.next <- newnext
;;

let l = single (0, 0);;
    
let rec adder cnt =
  add (cnt, 0) l;
  if cnt > 0 then adder (cnt - 1)
in adder 1000;;
    
let rec iterator a cnt alst =
  if cnt = 0 then a else
  let (i, _) = alst.data in
  iterator (a + i) (cnt - 1) alst.next
;;
    
let rec test cnt =
  ignore (iterator 0 1000 l);
  if cnt = 0 then () else test (cnt - 1)
;;

test 1000000;;


[-- Attachment #2: Type: application/pgp-signature, Size: 254 bytes --]

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

end of thread, other threads:[~2002-11-25 10:33 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-11-19 18:38 [Caml-list] Objects poor performance Cezary Kaliszyk
2002-11-20 11:50 ` Michal Moskal
2002-11-25 10:33 ` Xavier Leroy

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