From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Andy Wingo Newsgroups: gmane.lisp.guile.bugs Subject: Re: Loop optimization Date: Mon, 07 Mar 2011 22:36:40 +0100 Message-ID: References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: dough.gmane.org 1299533820 15665 80.91.229.12 (7 Mar 2011 21:37:00 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Mon, 7 Mar 2011 21:37:00 +0000 (UTC) Cc: Guile bug To: Michael Ellis Original-X-From: bug-guile-bounces+guile-bugs=m.gmane.org@gnu.org Mon Mar 07 22:36:52 2011 Return-path: Envelope-to: guile-bugs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Pwi6l-00022u-TK for guile-bugs@m.gmane.org; Mon, 07 Mar 2011 22:36:52 +0100 Original-Received: from localhost ([127.0.0.1]:45950 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Pwi6l-0004nd-EQ for guile-bugs@m.gmane.org; Mon, 07 Mar 2011 16:36:51 -0500 Original-Received: from [140.186.70.92] (port=55222 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Pwi6f-0004nN-4o for bug-guile@gnu.org; Mon, 07 Mar 2011 16:36:46 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Pwi6d-000251-Ly for bug-guile@gnu.org; Mon, 07 Mar 2011 16:36:45 -0500 Original-Received: from a-pb-sasl-sd.pobox.com ([64.74.157.62]:35761 helo=sasl.smtp.pobox.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Pwi6d-00024w-Fj for bug-guile@gnu.org; Mon, 07 Mar 2011 16:36:43 -0500 Original-Received: from sasl.smtp.pobox.com (unknown [127.0.0.1]) by a-pb-sasl-sd.pobox.com (Postfix) with ESMTP id 13DCC4E1B; Mon, 7 Mar 2011 16:38:08 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=pobox.com; h=from:to:cc :subject:references:date:in-reply-to:message-id:mime-version :content-type; s=sasl; bh=n+c41zrTtqWBu3LW0YGrpLO28hA=; b=Dra3LG bp0S8VnYdEXOmtg/qfzE8wtU0dt77BIWWG7YxibppJ4oLZkqfR13gKjY/GD76M/Q 6g+ksx3LER3THhHsgovCztrNQSpWFg7dmQJk0aTgdMBxgdcJAtG3hujYEOk7PSFI I8AdGbrWojnE3P+C8l1QBUAC4ZkcxrRjnewUQ= DomainKey-Signature: a=rsa-sha1; c=nofws; d=pobox.com; h=from:to:cc :subject:references:date:in-reply-to:message-id:mime-version :content-type; q=dns; s=sasl; b=uwWnYkOwOKcgNHhsZP7R57bqWyfRHaHD hVTZ1rtZqcsVgtc1XUBJhLREX9+AYpwWfVJp9MSmqmzEIbgv1FUg7Ks19aKO83Et g2k1cosqkdS/FmDKTWendIX506X5mhE631lgzqPjNHOe0/MN9+8KmoSLORC0bLHQ wggcQOhOufw= Original-Received: from a-pb-sasl-sd.pobox.com (unknown [127.0.0.1]) by a-pb-sasl-sd.pobox.com (Postfix) with ESMTP id F3E084E1A; Mon, 7 Mar 2011 16:38:05 -0500 (EST) Original-Received: from unquote.localdomain (unknown [90.164.198.39]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by a-pb-sasl-sd.pobox.com (Postfix) with ESMTPSA id 694B84E18; Mon, 7 Mar 2011 16:38:03 -0500 (EST) In-Reply-To: (Michael Ellis's message of "Mon, 7 Mar 2011 16:01:46 -0500") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux) X-Pobox-Relay-ID: 2FF1057E-4903-11E0-87E7-B86344810875-02397024!a-pb-sasl-sd.pobox.com X-detected-operating-system: by eggs.gnu.org: Solaris 10 (beta) X-Received-From: 64.74.157.62 X-BeenThere: bug-guile@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Bug reports for GUILE, GNU's Ubiquitous Extension Language" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: bug-guile-bounces+guile-bugs=m.gmane.org@gnu.org Errors-To: bug-guile-bounces+guile-bugs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.bugs:5307 Archived-At: On Mon 07 Mar 2011 22:01, Michael Ellis writes: > (let loop ((i 0) (j 1e7)) > (set! i (+ i 1)) > (if (< j i) > (begin (display i) (newline)) > (loop i j))) Note that if you avoid a set!, this gets faster: (let loop ((i 0) (j 1e7)) (if (<= j i) (begin (display i) (newline)) (loop (1+ i) j))) On my machine this goes down from 1.5 seconds to 0.91 seconds. See "Variables and the VM" in the manual, for more. You get better if you actually use an int as the boundary condition: (let loop ((i 0) (j 10000000)) (if (<= j i) (begin (display i) (newline)) (loop (1+ i) j))) 0.62 seconds here. In fact that is the deal: > (let loop ((i 0)) > (set! i (+ i 1)) > (if (< 1e7 i) > (begin (display i) (newline)) > (loop i))) Here we are loading up a float (which conses) at every iteration. Oddly enough this would work better in a procedure, because procedures have constant tables (again, "Variables and the VM"). So: (define (proc) (let loop ((i 0)) (set! i (+ i 1)) (if (< 1e7 i) (begin (display i) (newline)) (loop i)))) (proc) 1.19 seconds here. But the real thing is again to avoid the float<->int compare. By way of comparison: (let loop ((i 0)) (if (<= 10000000 i) (begin (display i) (newline)) (loop (1+ i)))) 0.56 seconds here. Regards, Andy -- http://wingolog.org/