unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Amirouche Boubekki <amirouche@hypermove.net>
To: Guile User <guile-user@gnu.org>
Cc: guile-user <guile-user-bounces+amirouche=hypermove.net@gnu.org>
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	[thread overview]
Message-ID: <691b8451eee86e60f7f1094488827073@hypermove.net> (raw)
In-Reply-To: <f1a6e788ede369278b8582188b571368@hypermove.net>

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!




  reply	other threads:[~2018-02-25 22:39 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-25 14:16 optimizing lazy sequences: srfi-41 vs delayed continuation with values + promise vs delayed continuation with cons + lambda Amirouche Boubekki
2018-02-25 14:30 ` Amirouche Boubekki
2018-02-25 22:39   ` Amirouche Boubekki [this message]
2018-02-25 23:31     ` Amirouche Boubekki
2018-03-02 23:22 ` Mark H Weaver

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=691b8451eee86e60f7f1094488827073@hypermove.net \
    --to=amirouche@hypermove.net \
    --cc=guile-user-bounces+amirouche=hypermove.net@gnu.org \
    --cc=guile-user@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).