[plt-dev] range mini benchmark
On May 2, David Van Horn wrote:
> Here is a benchmark for a few implementations of `range':
>
> Nat Nat -> [Listof Nat]
> (range 0 5) => (list 0 1 2 3 4 5)
> (range 5 0) => (list 5 4 3 2 1 0)
>
> One uses build-list, another uses sequences. I threw in an
> implementation using an iterative build-list as well.
>
> I was surprised the build-list version beat out sequences -- thoroughly,
> with the iterative build-list being an improvement over the built in
> recursive one.
This:
(define (range-seq L H)
(for/list ((i (if (>= H L)
(in-range L (+ H 1))
(in-range L (- H 1) -1)))) i))
forces creating a sequence value, and this:
(define (range-seq L H)
(for/list ((i (in-range L (+ H 1)))) i))
doesn't, so it is much faster.
Also, this kind of test is tricky to measure -- the number that you'd
want is the runtime minus the GC time -- but only the GC for the
resulting list.
--
((lambda (x) (x x)) (lambda (x) (x x))) Eli Barzilay:
http://www.barzilay.org/ Maze is Life!