[plt-scheme] Making a fast list like sequence

From: David Van Horn (dvanhorn at ccs.neu.edu)
Date: Tue Apr 7 19:09:27 EDT 2009

Matthew Flatt wrote:
> At Tue, 07 Apr 2009 18:32:33 -0400, David Van Horn wrote:
>> 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?
> If you use use `car', `cdr', and `pair? instead of `first', `rest', and
> `(compose not empty?)', you should get the same performance as for
> lists.

This didn't improve the running time very much.  It's still not nearly 
as fast as just a direct list value.

Here are the timings for a list sequence built with car/cdr/pair?, a 
list-like structure sequence, a direct list, and an list in in-list:

cpu time: 2036 real time: 2327 gc time: 0
cpu time: 2179 real time: 2387 gc time: 0
cpu time: 686 real time: 720 gc time: 0
cpu time: 690 real time: 694 gc time: 0


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

(define-struct kons (first rest))
(define (ks->seq ks)
    (lambda ()
       (lambda (p) true)
       (lambda (p v) true)))))

(define ks (foldr make-kons empty ls))

(define (ls->seq ls)
    (lambda ()
       (lambda (p) true)
       (lambda (p v) true)))))

(define lseq (ls->seq ls))
(define kseq (ks->seq ks))

(time (for ([i lseq]) i))

(time (for ([i kseq]) i))

(time (for ([i ls]) i))

(time (for ([i (in-list ls)]) i))

Posted on the users mailing list.