caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Cezary Kaliszyk <ck189400@zodiac.mimuw.edu.pl>
To: caml-list@inria.fr
Subject: [Caml-list] Objects poor performance
Date: Tue, 19 Nov 2002 19:38:57 +0100	[thread overview]
Message-ID: <20021119183857.GB15185@zodiac.mimuw.edu.pl> (raw)


[-- 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 --]

             reply	other threads:[~2002-11-19 22:26 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-11-19 18:38 Cezary Kaliszyk [this message]
2002-11-20 11:50 ` Michal Moskal
2002-11-25 10:33 ` 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=20021119183857.GB15185@zodiac.mimuw.edu.pl \
    --to=ck189400@zodiac.mimuw.edu.pl \
    --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;
as well as URLs for NNTP newsgroup(s).