caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jason Hickey <jyh@cs.caltech.edu>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Re: Pervasives.compare output type
Date: Thu, 24 Mar 2005 14:07:24 -0800	[thread overview]
Message-ID: <42433A1C.2060203@cs.caltech.edu> (raw)
In-Reply-To: <d1vc0m$eps$1@sea.gmane.org>

Bardur Arantsson wrote:
> Actually, since integers in OCaml are limited to (n-1) bits where n=32
> or n=64 depending on architecture, overflow shouldn't be a problem. (The
> comments in byterun/compare.c also seem to agree with that.)
> 
> Even so, it would be very slow to the polymorphic compare to compare
> integers, so if one cares about efficiency, direct subtraction is
> preferable.
> 

This discussion just came up on the MetaPRL list.  Here is an excerpt.

---

By the way, I tried using subtraction for a comparison, and it failed 
miserably:(

...

It is easy to explain--transitivity breaks.  For example, using 
subtraction, the following two relations hold: (min_int < 0) and (0 < 
1); however (min_int > 1).  Oops...

Aleksey Nogin wrote some micro-benchmarks.  These are 
back-of-the-envelope, so don't take them as definitive.
> I wrote the following test:
> 
> ------------
> 
> open Printf
> open Unix
> 
> let f1  x       y = Pervasives.compare x y
> let f2 (x: int) y = Pervasives.compare x y
> let f3  x       y = if x=y then Pervasives.compare "s1" "s2" else if x < y then -1 else 1
> let f4 (x: int) y = if x=y then Pervasives.compare "s1" "s2" else if x < y then -1 else 1
> let f5  x       y = let i = Pervasives.compare x y in if i = 0 then Pervasives.compare "s1" "s2" else i
> let f6 (x: int) y = let i = Pervasives.compare x y in if i = 0 then Pervasives.compare "s1" "s2" else i
> let f7  x       y = match Pervasives.compare x y with 0 -> Pervasives.compare "s1" "s2" | i -> i
> let f8 (x: int) y = match Pervasives.compare x y with 0 -> Pervasives.compare "s1" "s2" | i -> i
> 
> let time name f =
>    let t1=Unix.times () in
>       for i = 10 to 30000000 do ignore(f i 20) done;
>       let t2=Unix.times () in
>          eprintf "Function %s: user time: %f; system time: %f\n%!" name (t2.tms_utime -. t1.tms_utime) (t2.tms_stime -. t1.tms_s
> time)
> 
> let time_all () =
>    time "f1" f1;
>    time "f2" f2;
>    time "f3" f3;
>    time "f4" f4;
>    time "f5" f5;
>    time "f6" f6;
>    time "f7" f7;
>    time "f8" f8
> 
> let () =
>    time_all ();
>    time_all ();
>    time_all ()
> 
> -----------------
> 
> and here are rhe approximate running times:
> 
> Function f1: user time: 1.060000; system time: 0.000000
> Function f2: user time: 0.520000; system time: 0.000000
> Function f3: user time: 2.970000; system time: 0.000000
> Function f4: user time: 0.340000; system time: 0.000000
> Function f5: user time: 1.110000; system time: 0.000000
> Function f6: user time: 0.570000; system time: 0.000000
> Function f7: user time: 1.110000; system time: 0.000000
> Function f8: user time: 0.550000; system time: 0.000000
> 
> -- 
> Aleksey Nogin 

Jason

-- 
Jason Hickey                  http://www.cs.caltech.edu/~jyh
Caltech Computer Science      Tel: 626-395-6568 FAX: 626-792-4257


  reply	other threads:[~2005-03-24 22:07 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-03-24 18:47 Alex Baretta
2005-03-24 19:41 ` [Caml-list] " Richard Jones
2005-03-24 21:00   ` Marcin 'Qrczak' Kowalczyk
2005-03-24 21:38     ` Bardur Arantsson
2005-03-24 22:07       ` Jason Hickey [this message]
2005-03-24 22:26         ` [Caml-list] " brogoff
2005-03-25  9:42         ` Alex Baretta
2005-04-01  5:59           ` Aleksey Nogin
2005-03-24 22:15       ` Marcin 'Qrczak' Kowalczyk
2005-03-24 22:41         ` Bardur Arantsson
2005-03-25  9:43         ` [Caml-list] " Alex Baretta
2005-03-29  7:14 ` [Caml-list] " Oliver Bandel
2005-03-30 14:17 ` Xavier Leroy
2005-03-30 14:45   ` Alex Baretta
2005-03-30 15:11     ` Jacques Carette
2005-03-30 15:28       ` Alex Baretta
2005-03-30 17:47       ` brogoff
2005-03-30 18:21         ` Jacques Carette
2005-03-30 18:49           ` brogoff
2005-03-30 20:06             ` Jon Harrop
2005-03-30 20:43               ` Jacques Carette
2005-03-30 22:14                 ` Christopher Dutchyn
2005-03-31  0:44                 ` brogoff
2005-03-30 22:43             ` GADT?? (Re: [Caml-list] Pervasives.compare output type) Oliver Bandel
2005-03-30 22:35     ` [Caml-list] Pervasives.compare output type Oliver Bandel

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=42433A1C.2060203@cs.caltech.edu \
    --to=jyh@cs.caltech.edu \
    --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).