[racket] deceptive perfomance for typed racket in integer division

From: Pierpaolo Bernardi (olopierpa at gmail.com)
Date: Tue Dec 11 10:45:55 EST 2012

On Tue, Dec 11, 2012 at 4:03 PM, daniel rupis
<danielrupistralizavez at yahoo.es> wrote:
>
>  I was comparing some code in Qi with that of sbcl, I posted a question in
> comp.lang.lisp asking for a way to improve the perfomance, WJ gave a typed
> racket version that was slower than sbcl and also much slower than cpp.

The sbcl version is unsafe and uses numbers declared to be in a small range.

To not compare apples to pears, please measure the version below which
uses unsafe features of racket similar to the ones in the sbcl
version.

On my machine it produces:

c:\Users\bernardip\Documents\Scheme>testp
3340
cpu time: 2594 real time: 2685 gc time: 0

Remember to generate an executable, to obtain the maximum speed.

Alternatively, compare your racket version against sbcl with (declare
(optimize safety 3))

Cheers
P.

====

#lang racket

(require racket/unsafe/ops)

(define (divisible-rec i j)
  (cond ((unsafe-fx= j 1)
         #f)
        ((unsafe-fx= 0 (unsafe-fxmodulo i j))
         #t)
        (else
         (divisible-rec i (unsafe-fx+ -1 j)))))

(define (divisible n)
  (divisible-rec n (unsafe-fx+ -1 n)))

(define (total-primes k)
  (for/sum ([n (in-range 2 (add1 k))])
    (if (divisible n)
      0
      1)))

(define (test)
  (time (displayln (total-primes 30999))))

(test)

========

Posted on the users mailing list.