[Top][All Lists]

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

Re: Is this a good use for "compile"

From: Vítor De Araújo
Subject: Re: Is this a good use for "compile"
Date: Mon, 19 Feb 2018 21:08:25 -0300
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0

On 18/02/2018 18:56, Mark Carter wrote:
New scheme user here.

Suppose I'm writing a spreadsheet. The user inputs a formula for a cell.

The plan is to use guile's peg parser to convert the formula into a lambda expression, which I then compile in order to speed-up subsequent processing.

So, suppose I convert the user's formula to a list, which turns out to be, for example: '(lambda (x) (+ x 13)) and compile it and save it in a formula table:

(hash-set! my-cell-formulae some-cell-ref (compile '(lambda (x) (+ x 13))))

So I can I expect a speed-up by having done the compile, as opposed to an eval?

I assume the answer is "yes", but I wanted to check.

We can try this out:

scheme@(guile-user)> (use-modules (system base compile))
scheme@(guile-user)> (define exp '(lambda (n)
                                    (let loop ([i n] [total 0])
                                      (if (= i 0)
                                        (loop (1- i) (+ i total))))))
scheme@(guile-user)> (define f1 (eval exp (interaction-environment)))
scheme@(guile-user)> (define f2 (compile exp #:env (interaction-environment)))
scheme@(guile-user)> ,time (f1 1000000)
$2 = 500000500000
;; 0.845240s real time, 0.895351s run time.  0.071494s spent in GC.
scheme@(guile-user)> ,time (f2 1000000)
$3 = 500000500000
;; 0.067317s real time, 0.067278s run time.  0.000000s spent in GC.

So the answer does seem to be "yes": the compiled procedure is much faster.

Vítor De Araújo

reply via email to

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