[plt-scheme] scribblings on PLT Scheme 4.0
At Mon, 28 May 2007 09:44:38 +1000, Andrew Reilly wrote:
> I'm new to scheme myself, so I probably sholdn't have been
> making such liberal use of append! in my own code. I will
> repent...
Do you used them for performance reasons?
I used `append!' and `reverse!' a lot in my early Scheming, too,
because they were faster. It bit me often enough (no surprise) that I
decided to stay away, though there are certainly cases where mutating
versions work fine.
These days, it helps that we have a reasonable memory manager. While
`append!' and `reverse!' are still faster in 3m than `append' and
`reverse', allocation and GC are so much faster that I don't worry as
much about the extra pairs (which is as it should be). In the
microbenchmark below, the `append!' variants run almost 2x as fast in
both 3m and CGC, but 3m is about 4x as fast as CGC.
Matthew
----------------------------------------
(module time-it mzscheme
(define vec (make-vector 100))
(define (time-it append)
(time (let loop ([n 400000])
(unless (zero? n)
(append (vector->list vec)
(list #f))
(loop (sub1 n))))))
(time-it append)
(time-it append!)
(time-it (lambda (x r) (reverse x)))
(time-it (lambda (x r) (reverse! x))))