[gambit-list] number->integer
Marc Feeley
feeley at iro.umontreal.ca
Wed Oct 29 22:32:38 EDT 2008
On 29-Oct-08, at 8:49 PM, Guillaume Cartier wrote:
> If you know your flonum is within the range of fixnums you can use
> the very efficient ##flonum->fixnum which will basically compile to
> a cast (correct me Marc if i'm wrong).
There are several ways to truncate an inexact real to an exact
integer. Here are a few I can think of with their execution time on a
2 GHz Intel Core Duo when converting the number 152.97 . The last one
is over 230 times faster than the first! Of course they are not
completely equivalent because they have different constraints on the
argument to convert (some assume the result fits in a fixnum, others
in a signed 64 bit exact integer, and the first two have no constraint).
Marc
(define (f0 x) ;; 4600 ns per call
(declare (standard-bindings))
(inexact->exact (truncate x)))
(define (f1 x) ;; 4000 ns per call
(declare (standard-bindings))
(truncate (inexact->exact x)))
(define (f2 x) ;; 85 ns per call
((c-lambda (double) int64 "___result = ___arg1;") x))
(define (f3 x) ;; 25 ns per call
(declare (extended-bindings))
(##c-code "___RESULT = ___FIX(___F64UNBOX(___ARG1));" x))
(define (f4 x) ;; 20 ns per call
(declare (extended-bindings) (not safe))
(##fl->fx x))
More information about the Gambit-list
mailing list