Skip to content

Instantly share code, notes, and snippets.

@shirok
Created December 31, 2013 14:19
Show Gist options
  • Save shirok/8197416 to your computer and use it in GitHub Desktop.
Save shirok/8197416 to your computer and use it in GitHub Desktop.
(define (map_ proc lis)
(map1 proc lis 0))
(define (map1 proc lis n)
(cond [(null? lis) '()]
[(> n 30) (reverse (fold (^[elt r] (cons (proc elt) r)) '() lis))]
[else (cons (proc (car lis)) (map1 proc (cdr lis) (+ n 1)))]))
#|
gosh> (use gauche.time)
#<undef>
gosh> (define data (iota 30))
data
gosh> (time-these/report '(cpu 5) `((tail+reverse . ,(cut map - data))
(non-tail-rec . ,(cut map_ - data))))
Benchmark: ran tail+reverse, non-tail-rec, each for at least 5 cpu seconds.
tail+reverse: 5.049 real, 5.060 cpu (5.060 user + 0.000 sys)@359966.60/s n=1821431
non-tail-rec: 5.044 real, 5.080 cpu (5.080 user + 0.000 sys)@369097.44/s n=1875015
Rate tail+reverse non-tail-rec
tail+reverse 359967/s -- 0.975
non-tail-rec 369097/s 1.025 --
#<undef>
|#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment