Version française
Home     About     Download     Resources     Contact us    
Browse thread
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: -- (:)
From: Bardur Arantsson <spam@s...>
Subject: Re: Pervasives.compare output type
Marcin 'Qrczak' Kowalczyk wrote:
> Bardur Arantsson <spam@scientician.net> writes:
> 
> 
>>>>Wouldn't it be for speed?  You could define Pervasives.compare over
>>>>integers just as a subtraction.
>>>
>>>You can't, because of overflow.
>>
>>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.
> 
> 
> compare must eventually return an OCaml int. It can use subtraction
> only in its internal recursion, but when presenting the result to
> OCaml code it can't just pass the result of subtraction.
> 
> It can use subtraction internally no matter whether the OCaml
> interface uses intergers whose sign only matters, or an enumeration.
> 
> And thus there seems to be no performance advantage in using ints
> instead of the enumeration.
> 
> In practice it returns -1,0,1 anyway:
> # compare 10 20;;
> - : int = -1
> 

Indeed, I mistook compare_val for caml_compare.

Still if you can make sufficient guarantees about the ranges of the
integers being compared, you *can* use '-' which *should* be faster than
branching according to conventional wisdom.

The more 'relaxed' requirement that comparison functions can return
anything<0, 0 or anything>0 just means that any higher-order functions
which take comparison functions as arguments *might* run slightly
faster... whether this is true in practise is another matter entirely.

Cheers,

-- 
Bardur Arantsson
<bardur@imada.sdu.dk>
<bardur@scientician.net>

It's not often you see something that's both romantic *and*
thrifty.
                                               Dawn, 'The Office'