[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: optimizing lazy sequences: srfi-41 vs delayed continuation with valu
From: |
Amirouche Boubekki |
Subject: |
Re: optimizing lazy sequences: srfi-41 vs delayed continuation with values + promise vs delayed continuation with cons + lambda |
Date: |
Sun, 25 Feb 2018 23:39:05 +0100 |
User-agent: |
Roundcube Webmail/1.1.2 |
I figured how to benchmark this.
Here are the timings:
promise: 43s
lambda: 7s
And at the current 'max' value the srfi-41 streams can't complete
the benchmark with this error:
Too many heap sections: Increase MAXHINCR or MAX_HEAP_SECTS
Here is the benchmark program:
(use-modules (srfi srfi-41))
(define max (expt 10 8))
(define (time-it thunk)
(let ((start (current-time)))
(thunk)
(- (current-time) start)))
;; promise based lazy seq
(define (lazyseq-with-promise)
(let loop ((v 1))
(delay
(values v (loop (+ 1 v))))))
(define (consume-promise promise)
(let loop ((v 0)
(promise promise))
(unless (eq? v max)
(call-with-values (lambda () (force promise))
(lambda (v next)
(loop (+ 1 v) next))))))
(define v1 (time-it (lambda () (consume-promise
(lazyseq-with-promise)))))
;; lambda based lazy seq
(define (lazyseq-with-lambda)
(let loop ((v 1))
(lambda ()
(values v (loop (+ 1 v))))))
(define (consume-lambda thunk)
(let loop ((v 0)
(thunk thunk))
(unless (eq? v max)
(call-with-values thunk
(lambda (v n)
(loop (+ 1 v) n))))))
(define v2 (time-it (lambda () (consume-lambda (lazyseq-with-lambda)))))
(display "promise: ")
(display v1)
(newline)
(display "lambda: ")
(display v2)
(newline)
(define (lazyseq-with-stream)
(list->stream (iota max)))
(define (consume-stream stream)
(let loop ((v 0)
(stream stream))
(unless (eq? v max)
(loop (+ 1 v) (stream-cdr stream)))))
(define stream (lazyseq-with-stream))
(define v3 (time-it (lambda () (consume-stream stream))))
(display "stream: ")
(display v3)
(newline)
Happy hacking!