Created
July 25, 2018 16:50
-
-
Save mflatt/3b760d019d5e8e74125bf13e94e5945f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket/base | |
(define M 100) | |
(define N 1000000) | |
;; Baseline tail-call loop | |
(time | |
(let loop ([j 0]) | |
(cond | |
[(= j N) 'done] | |
[else | |
(let loop ([i 0]) | |
(cond | |
[(= i M) 'done] | |
[else (loop (add1 i))])) | |
(loop (add1 j))]))) | |
;; WCM around tail call | |
(time | |
(let loop ([j 0]) | |
(cond | |
[(= j N) 'done] | |
[else | |
(let loop ([i 0]) | |
(cond | |
[(= i M) 'done] | |
[else (with-continuation-mark | |
'k i | |
(loop (add1 i)))])) | |
(loop (add1 j))]))) | |
;; ---------------------------------------- | |
;; Baseline non-tail-call recursion | |
(time | |
(let loop ([j 0]) | |
(cond | |
[(= j N) 'done] | |
[else | |
(let loop ([i 0]) | |
(cond | |
[(= i M) i] | |
[else (sub1 (loop (add1 i)))])) | |
(loop (add1 j))]))) | |
;; WCM non-tail-call recursion | |
(time | |
(let loop ([j 0]) | |
(cond | |
[(= j N) 'done] | |
[else | |
(let loop ([i 0]) | |
(cond | |
[(= i M) 0] | |
[else (with-continuation-mark | |
'k i | |
(sub1 (loop (add1 i))))])) | |
(loop (add1 j))]))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment