Consider this bit of Chez Scheme code:
(import (chezscheme)) (define (list-enumerate ls val proc) (let loop ((ls ls) (return? #f) (val val)) (if (or (null? ls) return?) val (call-with-values (lambda () (proc val (car ls))) (lambda (return? val) (loop (cdr ls) return? val)))))) (define (list-index ls proc) (list-enumerate ls 0 (lambda (i elt) (if (proc elt) (values #t i) (values #f (+ i 1)))))) (define n 100000) (define data (iota n)) (time (list-index data (lambda (elt) (= elt (- n 1)))))
Run it:
~ $ scheme --script ~/scratch/_list-enumerate-allocation-test-chez-a.sps (time (list-index data ...)) no collections 3 ms elapsed cpu time 4 ms elapsed real time 8 bytes allocated
Wow, it reports that only 8 bytes were allocated.
Let's run it again using the --program
option instead of --script
:
~ $ scheme --program ~/scratch/_list-enumerate-allocation-test-chez-a.sps (time (list-index data ...)) no collections 3 ms elapsed cpu time 3 ms elapsed real time 800000 bytes allocated
Yikes, 800000 bytes allocated.
What's up with the difference?
Ed