Last active
February 18, 2021 07:53
-
-
Save shhyou/05b33363502014c25b77bab3004ebf22 to your computer and use it in GitHub Desktop.
This file contains 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 | |
(require racket/generator | |
racket/stream | |
racket/sequence) | |
(define (make-iter-gen xs ys) | |
(generator | |
() | |
(for ([x (in-list xs)]) | |
(yield x)) | |
(for ([y (in-list ys)]) | |
(yield y)) | |
#f)) | |
(define (make-iter-state xs ys) | |
(λ () | |
(cond | |
[(pair? xs) | |
(begin0 | |
(car xs) | |
(set! xs (cdr xs)))] | |
[(pair? ys) | |
(begin0 | |
(car ys) | |
(set! ys (cdr ys)))] | |
[else | |
#f]))) | |
(displayln "\ngen") | |
(time | |
(let ([it! (make-iter-gen | |
(build-list 1000000 (λ (n) n)) | |
(build-list 700000 (λ (n) n)))]) | |
(let loop ([sum 0]) | |
(cond | |
[(it!) => (λ (n) (loop (+ n sum)))] | |
[else sum])))) | |
(displayln "\nstate") | |
(time | |
(let ([it! (make-iter-state | |
(build-list 1000000 (λ (n) n)) | |
(build-list 700000 (λ (n) n)))]) | |
(let loop ([sum 0]) | |
(cond | |
[(it!) => (λ (n) (loop (+ n sum)))] | |
[else sum])))) | |
(displayln "\nstream-box") | |
(time | |
(let ([b (box | |
(stream-append | |
(in-list (build-list 1000000 (λ (n) n))) | |
(in-list (build-list 700000 (λ (n) n)))))]) | |
(let loop ([sum 0]) | |
(define s (unbox b)) | |
(cond | |
[(not (stream-empty? s)) | |
(define n (stream-first s)) | |
(set-box! b (stream-rest s)) | |
(loop (+ n sum))] | |
[else sum])))) | |
(displayln "\nsequence with append") | |
(time | |
(let-values ([(has-next? it!) | |
(sequence-generate | |
(append | |
(build-list 1000000 (λ (n) n)) | |
(build-list 700000 (λ (n) n))))]) | |
(let loop ([sum 0]) | |
(cond | |
[(has-next?) (loop (+ (it!) sum))] | |
[else sum])))) | |
(displayln "\nsequence with sequence-append") | |
(time | |
(let-values ([(has-next? it!) | |
(sequence-generate | |
(sequence-append | |
(build-list 1000000 (λ (n) n)) | |
(build-list 700000 (λ (n) n))))]) | |
(let loop ([sum 0]) | |
(cond | |
[(has-next?) (loop (+ (it!) sum))] | |
[else sum])))) |
This file contains 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
$ racket -vt- lazy-list-append.rkt | |
Welcome to Racket v8.0.0.6 [cs]. | |
gen | |
cpu time: 1216 real time: 1325 gc time: 243 | |
744999150000 | |
state | |
cpu time: 185 real time: 199 gc time: 135 | |
744999150000 | |
stream-box | |
cpu time: 1040 real time: 1129 gc time: 114 | |
744999150000 | |
sequence with append | |
cpu time: 282 real time: 306 gc time: 106 | |
744999150000 | |
sequence with sequence-append | |
cpu time: 601 real time: 650 gc time: 84 | |
744999150000 | |
$ racket -vt- lazy-list-append.rkt | |
Welcome to Racket v7.9 [bc]. | |
gen | |
cpu time: 7979 real time: 8307 gc time: 391 | |
744999150000 | |
state | |
cpu time: 266 real time: 273 gc time: 126 | |
744999150000 | |
stream-box | |
cpu time: 940 real time: 978 gc time: 153 | |
744999150000 | |
sequence with append | |
cpu time: 583 real time: 620 gc time: 186 | |
744999150000 | |
sequence with sequence-append | |
cpu time: 910 real time: 945 gc time: 185 | |
744999150000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment