[racket] Loops and performance

From: Sergi Mansilla (sergi.mansilla at gmail.com)
Date: Sat Dec 3 13:31:51 EST 2011

Hi all,

I have been a developer for a long time, but I am just starting with
Racket (but not lisp-based languages). One of my first experiments has
been to make a Racket equivalent of the simple looping code in this
blog article: https://scottlocklin.wordpress.com/2011/11/30/only-fast-languages-are-interesting.
After failing it to make it execute anywhere near the speeds that the
author is getting with his 'lush' language, I asked for help at
StackOverflow, and Sam Tobin-Hochstadt very kindly helped me out and
came with the following implementation:

#lang typed/racket

(require racket/flonum)

(define N 3000000)
(define avector
    (for/flvector #:length N ([i (in-range N)]) (random)))

(: sum-vec : FlVector -> Float)
(define (sum-vec v)
  (for/fold ([i 0.0]) ([e (in-flvector v)]) (+ e i)))

(time (sum-vec avector))

The code above effectively achieves the same speed (20ms) that the
author of the blog post achieves. That made me happy.

After that, I implemented two other versions of this code, one in C
(which obviously ran much faster, in 3ms), and another one in
Node.js/JavaScript. To my surprise, the JavaScript code ran in 10ms,
faster than the Racket code. It is probably because of the crazy
optimizations that the V8 engine has.

Anyway, my newbie question is: Is there a way to execute that code
faster? Perhaps I am in some debug mode, or I can pass parameters to
racket so it does some optimizations? I am just curious and just
kicking the tires of Racket. I am not even that interested in high
performance, but I'd like to know if I am missing something.

Thanks a lot,

Sergi Mansilla


Posted on the users mailing list.