chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] numbers egg slow?


From: Alex Shinn
Subject: Re: [Chicken-users] numbers egg slow?
Date: Thu, 06 Oct 2005 01:29:08 -0500
User-agent: Wanderlust/2.10.1 (Watching The Wheels) SEMI/1.14.6 (Maruoka) FLIM/1.14.6 (Marutamachi) APEL/10.6 Emacs/21.3 (i386-pc-linux-gnu) MULE/5.0 (SAKAKI)

At Thu, 06 Oct 2005 14:50:18 +0900, Daishi Kato wrote:
> 
> That is certainly understandable, except that I do not know if
> type-checking in scheme code is really slower than type-cheking
> in c code, if the scheme code is compiled properly or unsafely. (or inlined?)
> # Basically, there should not be any overhead from the code
> #   IF fixnum? THEN do_fixnum_plus ELSE do_flonum_plus
> # to the code
> #   IF fixnum? THEN do_fixnum_plus ELSE IF flonum? do_flonum_plus
> #   ELSE IF bignum? do_bignum_plus ELSE IF ratnum? do_ratnum_plus
> #   ELSE do_compnum_plus
> # ,when doing plus on fixnums.
> # Is it true?
> 
> I also tried to compare with fx+. How would you explain this?
> 
> % csi -eval '(print (cpu-time))(let loop ([i 0]) (or (> i (expt 2 17)) (loop 
> (fx+ i 1))))(print (cpu-time))'           
> 2
> 107
> % csi -eval '(use numbers)(print (cpu-time))(let loop ([i 0]) (or (> i (expt 
> 2 17)) (loop (fx+ i 1))))(print (cpu-time))'
> 3
> 992

You have to be careful what you're measuring.  Inside the loop you
repeatedly call expt, which is slower not only because it makes many
more checks than before, such as checking the types and signs of the
arguments, but because it checks the fixnum case last.

If you bind the result once outside the loop you'll see the time using
fx+ (and also fx> to be sure we're comparing the same thing) is indeed
exactly the same:

$ csi -eval '(let ((2^17 (expt 2 17)))(gc)(time (let loop ([i 0]) (or (fx> i 
2^17) (loop (fx+ i 1))))))'
   0.313 seconds elapsed
       0 seconds in (major) GC
      31 mutations
    1285 minor GCs
       0 major GCs

$ csi -R numbers -eval '(let ((2^17 (expt 2 17)))(gc)(time (let loop ([i 0]) 
(or (fx> i 2^17) (loop (fx+ i 1))))))'
   0.313 seconds elapsed
       0 seconds in (major) GC
      31 mutations
    1285 minor GCs
       0 major GCs

Now let's compare the time of the generic numbers + and normal +:

$ csi -eval '(let ((2^17 (expt 2 17)))(gc)(time (let loop ([i 0]) (or (> i 
2^17) (loop (+ i 1))))))'
   0.325 seconds elapsed
       0 seconds in (major) GC
      31 mutations
    1289 minor GCs
       0 major GCs

$ csi -R numbers -eval '(let ((2^17 (expt 2 17)))(gc)(time (let loop ([i 0]) 
(or (> i 2^17) (loop (+ i 1))))))'
   0.454 seconds elapsed
   0.002 seconds in (major) GC
      31 mutations
      58 minor GCs
       1 major GCs

0.454 vs 0.325, or about 1.4x slower when using the full number tower.

-- 
Alex




reply via email to

[Prev in Thread] Current Thread [Next in Thread]