From: Brian <gomesbascoy@gmail.com>
To: guile-devel@gnu.org
Subject: Performance impact of top level definitions
Date: Tue, 08 May 2018 22:19:23 -0700 [thread overview]
Message-ID: <542e7f030ad122e035a353f44465d4d3158d7f8c.camel@gmail.com> (raw)
Hello,
Today I found that top level defines have a significant performance
impact on Guile (2.2.3). The following program takes about 108 seconds
to complete on my ThinkPad (an i5-5200U with Arch Linux):
(define node cons)
(define node-left car)
(define node-right cdr)
(define (make d)
(if (= d 0)
(node #f #f)
(let ([d2 (1- d)])
(node (make d2) (make d2)))))
(define (check t)
(if (node-left t)
(+ 1 (check (node-left t)) (check (node-right t)))
1))
(define (main n)
(define min-depth 4)
(define max-depth (max (+ min-depth 2) n))
(define stretch-depth (1+ max-depth))
(format #t "stretch tree of depth ~a\t check: ~a\n" stretch-depth
(check (make stretch-depth)))
(let ([long-lived-tree (make max-depth)])
(do ([d 4 (+ d 2)]) ([not (< d (1+ max-depth))])
(let ([iterations (ash 1 (+ (- max-depth d) min-depth))])
(format #t "~a\t trees of depth ~a\t check: ~a\n"
iterations
d
(let sum ([i iterations] [n 0])
(if (zero? i)
n
(sum (1- i) (+ n (check (make d)))))))))
(format #t "long lived tree of depth ~a\t check: ~a\n"
max-depth
(check long-lived-tree))))
(main 21)
By simply wrapping that code in a lambda the program finished in about
47 seconds. Using lets instead of defines is equally effective.
I was quite surprised because I initially thought some optimization
would just substitute those useless nodes symbols away, but it seems
like that's not the case...
Cheers!
next reply other threads:[~2018-05-09 5:19 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-05-09 5:19 Brian [this message]
2018-05-15 2:55 ` Performance impact of top level definitions Mark H Weaver
2018-05-15 16:55 ` Brian
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=542e7f030ad122e035a353f44465d4d3158d7f8c.camel@gmail.com \
--to=gomesbascoy@gmail.com \
--cc=guile-devel@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).