[plt-scheme] Iterator performance

From: Yavuz Arkun (yarkun at gmail.com)
Date: Fri Jul 4 18:20:07 EDT 2008

Hmm, I copy/pasted your code into my PLT and ran it:

cpu time: 13 real time: 13 gc time: 0
cpu time: 13 real time: 13 gc time: 0
cpu time: 55 real time: 58 gc time: 0
cpu time: 20 real time: 21 gc time: 0

This is on OS X 10.5.4, version:

Welcome to DrScheme, version 4.0.2.3-svn3jul2008 [3m].
Language: Module custom; memory limit: 512 megabytes.

(I also ran with memory limited to 128MB; essentially same runtimes.)
Seems that I don't see such a drastic difference.
--Yavuz

2008/7/5 Mark Engelberg <mark.engelberg at gmail.com>:
> I've noticed that iterators are fast.  So fast, in fact, that I can't
> seem to approach their speed with any Scheme-written loopy version.
> I've tested both vectors and lists, and gotten similar results.
>
> Here is some sample benchmarking on vector iteration:
> #lang scheme
> (define v (build-vector 500000 (λ (i) i)))
>
> (collect-garbage)
> (define list1 (time (for/list ([item (in-vector v)]) item)))
> (set! list1 #f)
> (collect-garbage)
> (define list2 (time (for/list ([index (in-range (vector-length v))])
> (vector-ref v index))))
> (set! list2 #f)
> (collect-garbage)
> (define (v->l v i)
>  (if (= i (vector-length v)) empty
>      (cons (vector-ref v i) (v->l v (add1 i)))))
> (define list3 (time (v->l v 0)))
> (set! list3 #f)
> (define (v->l2 v i l)
>  (if (= i (vector-length v)) (reverse l)
>      (v->l2 v (add1 i) (cons (vector-ref v i) l))))
> (define list4 (time (v->l2 v 0 empty)))
>
> On my computer:
> cpu time: 16 real time: 16 gc time: 0
> cpu time: 78 real time: 78 gc time: 0
> cpu time: 500 real time: 500 gc time: 156
> cpu time: 250 real time: 250 gc time: 0
>
> Is all iteration done on the C side?  If not, what wizardry makes it
> so much faster?
>
> It's enough of a difference to make me want to do iteration all the
> time.  Forget this recursion stuff :) ...
>
> Speaking of which, if I make a data structure into a sequence using
> prop:sequence, it works great in for loops, but what do I need to do
> to make it behave like the superfast iterators, such as in-list,
> in-vector, etc.?
>
> --Mark
>
> _________________________________________________
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
>

Posted on the users mailing list.