[racket-dev] speeding up 16-bit integer adds

From: John Clements (clements at brinckerhoff.org)
Date: Thu Sep 23 22:42:15 EDT 2010

I'm trying to add together big buffers. The following code creates two big fat buffers of 16-bit integers, and adds them together destructively. It looks to me like this code *could* run really fast, but it doesn't; this takes about 8.5 seconds. Changing + to unsafe-fx+ has no detectable effect.  Is there allocation going on in the inner loop? I'd hoped that since an _sint16 fits safely in 31 bits, that no memory would be allocated in the inner loop. Grr! Any suggestions? (I ran a similar test on floats, and C ran about 64x faster, about a tenth of a second).

Doc pointers appreciated as always,


#lang racket 

(require ffi/unsafe)

(define (make-buffer-of-small-random-ints len)
  (let ([buf (malloc _sint16 len)])
    (for ([i (in-range len)])
      (ptr-set! buf _sint16 i 73))

(define buf-len (* 44100 2 200))

(define b1 (make-buffer-of-small-random-ints buf-len))
(define b2 (make-buffer-of-small-random-ints buf-len))

 (for ([i (in-range buf-len)])
   (ptr-set! b1 _sint16 i 
             (+ (ptr-ref b1 _sint16 i)
                (ptr-ref b2 _sint16 i)))))
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4669 bytes
Desc: not available
URL: <http://lists.racket-lang.org/dev/archive/attachments/20100923/1cd0d4ab/attachment.p7s>

Posted on the dev mailing list.