caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: benjamin.canou@gmail.com
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Strange performances
Date: Fri, 18 Jan 2008 11:28:48 +0900 (JST)	[thread overview]
Message-ID: <20080118.112848.170122253.garrigue@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <20080118.111503.185813743.garrigue@math.nagoya-u.ac.jp>

Sorry, my explanation was wrong.
Actually, your error was much worse.
You apparently assumed that s.[i] :: list_of_string s (succ i)
would be evaluated from left to write, raising an exception
as soon as i gets out of s. But this is not the case!
It is evaluated from right to left, first looping until the stack
overflows, then raising exceptions on all the way back. No surprise
that this was incredibly slow. And my segmentation fault was just
caused by the stack overflow. Otherwise, out-of-bound accesses are
correctly reported.

Jacques Garrigue

From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
> The problem seems to be in list_of_string.
> If I write:
> 
> let list_of_string s =
>   let rec list_of_string s i =
>     if i < String.length s then s.[i] :: list_of_string s (succ i) else []
>   in list_of_string s 0
> 
> I get a 10 time increase in speed for bytecode, and 5 times better for
> native code, as one would expect. Out-of-bounds exceptions are
> intended as fatal errors, do not try to catch them...
> 
> By the way, on my machine your version doesn't even work in native
> code, I only get segfaults. This is allowed behaviour for
> out-of-bounds access.
> 
> Jacques Garrigue
> 
> From: Benjamin Canou <benjamin.canou@gmail.com>
> >   Hi,
> > 
> > The code following my message is way faster in bytecode than in native
> > code. Is there a good reason for that or is it a bug ?
> > Note : It is a (way too, I know) naive implementation of the well known
> > string suite 1, 11, 21, 1211, 111221, ...
> > 
> >   Benjamin Canou.
> > 
> > === code ===
> > 
> > let list_of_string s =
> >   let rec list_of_string s i =
> >     try s.[i] :: list_of_string s (succ i)
> >     with _ -> []
> >   in list_of_string s 0
> > 
> > let rec trans = function
> >   | '1' :: '1' :: '1' :: tl -> "31" ^ trans tl
> >   | '1' :: '1' :: tl -> "21" ^ trans tl
> >   | '1' :: tl -> "11" ^ trans tl
> >   | '2' :: '2' :: '2' :: tl -> "32" ^ trans tl
> >   | '2' :: '2' :: tl -> "22" ^ trans tl
> >   | '2' :: tl -> "12" ^ trans tl
> >   | '3' :: '3' :: '3' :: tl -> "33" ^ trans tl
> >   | '3' :: '3' :: tl -> "23" ^ trans tl
> >   | '3' :: tl -> "13" ^ trans tl
> >   | [] -> ""
> >   | _ -> failwith "bad input"
> > 
> > let rec print n s =
> >   print_endline s ;
> >   if n > 0 then print (pred n) (trans (list_of_string s))
> >     
> > let _ = print 30 "1"
> > 
> > === perfs ===
> > 
> > benjamin@benjamin-laptop:~/Work/Stuff$ ocamlopt 123.ml -o 123
> > benjamin@benjamin-laptop:~/Work/Stuff$ time ./123
> > [...]
> > real    0m5.245s
> > user    0m4.944s
> > sys     0m0.016s
> > benjamin@benjamin-laptop:~/Work/Stuff$ ocamlc 123.ml -o 123
> > benjamin@benjamin-laptop:~/Work/Stuff$ time ./123
> > [...]
> > real    0m1.097s
> > user    0m0.840s
> > sys     0m0.008s
> > benjamin@benjamin-laptop:~/Work/Stuff$ ocaml -version
> > The Objective Caml toplevel, version 3.09.2
> 
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs


  reply	other threads:[~2008-01-18  2:29 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-01-18  1:32 Benjamin Canou
2008-01-18  2:15 ` [Caml-list] " Jacques Garrigue
2008-01-18  2:28   ` Jacques Garrigue [this message]
2008-01-18  7:39   ` Till Varoquaux
2008-01-18  9:12     ` Jacques Garrigue
2008-01-18 16:55       ` Benjamin Canou
2008-01-18 17:05         ` Olivier Andrieu
2008-01-18 17:11           ` Jon Harrop
2008-01-18 17:43         ` Jon Harrop
2008-01-18 19:53           ` Benjamin Canou
2008-01-18 16:55       ` Edgar Friendly
2008-01-18 17:52         ` Kuba Ober
2008-01-18 17:56           ` Jon Harrop
2008-01-19  2:32         ` Jacques Garrigue
2008-01-24 22:52           ` Christophe Raffalli

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=20080118.112848.170122253.garrigue@math.nagoya-u.ac.jp \
    --to=garrigue@math.nagoya-u.ac.jp \
    --cc=benjamin.canou@gmail.com \
    --cc=caml-list@yquem.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).