[plt-scheme] Making a fast list like sequence

From: David Van Horn (dvanhorn at ccs.neu.edu)
Date: Tue Apr 7 18:32:33 EDT 2009

I see dramatic differences in running times between the built-in list 
sequence and a naive implementation of list sequencing.  What can I do 
to make this naive implementation run faster?  (I have a cons-like 
structure I want to provide a prop:sequence for, but I would like it to 
be nearly as fast as the real cons if possible).

Thanks,
David

#lang scheme
(define ls (build-list 100000 (lambda (i) i)))

(define (ls->seq ls)
   (make-do-sequence
    (lambda ()
      (values
       first
       rest
       ls
       (compose not empty?)
       (lambda (p) true)
       (lambda (p v) true)))))

(define seq (ls->seq ls))

(collect-garbage)
(time (for ([i seq]) (void i)))

(collect-garbage)
(time (for ([i ls])  (void i)))

cpu time: 2009 real time: 2032 gc time: 0
cpu time: 685 real time: 700 gc time: 0


Posted on the users mailing list.