caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Xavier Leroy <xavier.leroy@inria.fr>
To: Mattias Waldau <mattias.waldau@abc.se>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Beware of compare (and Ocaml beaten by Java)
Date: Tue, 27 Nov 2001 10:21:30 +0100	[thread overview]
Message-ID: <20011127102130.A16765@pauillac.inria.fr> (raw)
In-Reply-To: <AAEBJHFJOIPMMIILCEPBCECKDFAA.mattias.waldau@abc.se>; from mattias.waldau@abc.se on Mon, Nov 26, 2001 at 07:40:00PM +0100

> Why isn't compare compiled as '-'? According to the definition
> of compare this should be okay.

I assume you mean "compare at type int -> int -> int", because for
types represented as pointers, pointer subtraction wouldn't give
reliable results -- for one thing, the GC can move blocks around.

Even on integer arguments, "-" cannot be used due to arithmetic
overflow:

  compare min_int max_int = -1    (* correct *)
  min_int - max_int = 1           (* incorrect *)

So, replacing "compare" by "-" is only valid for small integer types
such as "char" and enumerated datatypes, and I felt this wasn't
important enough to implement.  (Given your example, you'll disagree,
of course.)

> The core of the slow program is
> 
> (* compare two substrings of the SAME text 
>    [compare x y] returns [0] if [x=y], a negative integer if
>    [x<y], and a positive integer if [x>y] *)
> let rec same_substr_compare str idx1 idx2 : int =
>   let length = String.length str in
>   (* shortest string is smaller *)
>   if idx1 = length then -1 else
>   if idx2 = length then 1 else 
>   (* compare one char *)
>   let res = compare str.[idx1] str.[idx2] in
>   (* char was equal, recurse *)
>   if res = 0 then same_substr_compare str (idx1+1) (idx2+1)
>   (* char was different, finished *)
>   else res ;;

I get the impression that same_substr_compare never returns 0; is this
intentional?

> 3. Mergesort (=Array.stable_sort) is faster than 
> heapsort (=Array.sort). (runtime down from 60s to 40s).
> (I also tried quicksort (=Sort.array), but after 8 hours
> it still hadn't finished.)

This is a bit surprising: you might have hit one of those cases where
Quicksort is O(n^2), but it could also be the case that Sort.array
malfunctions because your comparison function is not a proper
comparison function (it doesn't return 0 for equal things).

- Xavier Leroy
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


  reply	other threads:[~2001-11-27  9:21 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-10-02 17:53 [Caml-list] replay debugger Yoann Padioleau
2001-10-04 12:39 ` Xavier Leroy
2001-11-26 14:51   ` Fergus Henderson
2001-11-26 16:14     ` Xavier Leroy
2001-11-26 18:40     ` [Caml-list] Beware of compare (and Ocaml beaten by Java) Mattias Waldau
2001-11-27  9:21       ` Xavier Leroy [this message]
2001-11-27  9:41         ` Mattias Waldau
2001-11-30  9:12           ` Pierre Weis
2001-12-03 21:37             ` Chris Hecker
2001-11-27 17:03         ` [Caml-list] bytegen.comp_expr error when doing object copying Neil Inala
2001-11-28 20:15           ` 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=20011127102130.A16765@pauillac.inria.fr \
    --to=xavier.leroy@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=mattias.waldau@abc.se \
    /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).