[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Performance impact of top level definitions

From: Brian
Subject: Performance impact of top level definitions
Date: Tue, 08 May 2018 22:19:23 -0700


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)))

(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"
                (let sum ([i iterations] [n 0])
                  (if (zero? i)
                      (sum (1- i) (+ n (check (make d)))))))))
    (format #t "long lived tree of depth ~a\t check: ~a\n"
            (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...


reply via email to

[Prev in Thread] Current Thread [Next in Thread]