This site is updated infrequently. For up-to-date information, please visit the new OCaml website at ocaml.org.

Pervasives.compare output type
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: 2005-04-01 (05:59) From: Aleksey Nogin Subject: Re: [Caml-list] Re: Pervasives.compare output type
```On 25.03.2005 01:42, Alex Baretta wrote:

>>> 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 =
>>>
>
>
> I am unsure as to how to interpret these benchmarks...

Yes, these were taken a bit out of context. Basically, the question we
were discussing was:
> What's the fastest way to compare int*string pairs (or, in general - int*'a), where ints are likely to be distinct whenever the second elements are distinct (basically, the int is a hash)?

The answer is that the "f4" above is the fastest one:

let compair_pair ((i1:int), a1) (i2, a2) =
if i1 = i2 then
Pervasives.compare a1 a2
else if i1 < i2 then
-1
else
1

--
Aleksey Nogin