From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Amirouche Boubekki Newsgroups: gmane.lisp.guile.user 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 Message-ID: <691b8451eee86e60f7f1094488827073@hypermove.net> References: NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit X-Trace: blaine.gmane.org 1519598247 9682 195.159.176.226 (25 Feb 2018 22:37:27 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sun, 25 Feb 2018 22:37:27 +0000 (UTC) User-Agent: Roundcube Webmail/1.1.2 Cc: guile-user To: Guile User Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Sun Feb 25 23:37:23 2018 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eq4v4-0002Ew-PH for guile-user@m.gmane.org; Sun, 25 Feb 2018 23:37:22 +0100 Original-Received: from localhost ([::1]:56087 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eq4x7-0008JO-9e for guile-user@m.gmane.org; Sun, 25 Feb 2018 17:39:29 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:43310) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eq4wo-0008J2-Er for guile-user@gnu.org; Sun, 25 Feb 2018 17:39:11 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eq4wn-0003d1-Pd for guile-user@gnu.org; Sun, 25 Feb 2018 17:39:10 -0500 Original-Received: from relay5-d.mail.gandi.net ([2001:4b98:c:538::197]:45041) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eq4wl-0003c1-L0; Sun, 25 Feb 2018 17:39:07 -0500 Original-Received: from webmail.gandi.net (webmail7-d.mgt.gandi.net [10.58.1.147]) (Authenticated sender: amirouche@hypermove.net) by relay5-d.mail.gandi.net (Postfix) with ESMTPA id C436241C07D; Sun, 25 Feb 2018 23:39:05 +0100 (CET) In-Reply-To: X-Sender: amirouche@hypermove.net X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2001:4b98:c:538::197 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.org gmane.lisp.guile.user:14468 Archived-At: 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!