caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Mattias Waldau" <mattias.waldau@abc.se>
To: "Xavier Leroy" <xavier.leroy@inria.fr>
Cc: <caml-list@inria.fr>
Subject: RE: [Caml-list] Beware of compare (and Ocaml beaten by Java)
Date: Tue, 27 Nov 2001 10:41:10 +0100	[thread overview]
Message-ID: <AAEBJHFJOIPMMIILCEPBOEDGDFAA.mattias.waldau@abc.se> (raw)
In-Reply-To: <20011127102130.A16765@pauillac.inria.fr>

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

Yes.

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

Yes, you are right, overflow is a problem. Maybe using "-" instead of 
compare should just be a tip instead next to the documentation of compare.

> 
> > 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?

Since I compare substrings of the same string from different positions 
to the end of the string, there will never be any identical 
substrings (one of them will always be shorter).

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

It worked on smaller datasets, so I think I found an O(n^2)-case.
My Java-collegue had the same problem and had to implement merge sort.
I didn't look into it a lot.

> 
> - 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:41 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
2001-11-27  9:41         ` Mattias Waldau [this message]
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=AAEBJHFJOIPMMIILCEPBOEDGDFAA.mattias.waldau@abc.se \
    --to=mattias.waldau@abc.se \
    --cc=caml-list@inria.fr \
    --cc=xavier.leroy@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).