unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Is this a good use for "compile"
@ 2018-02-18 21:56 Mark Carter
  2018-02-19 17:30 ` Matt Wette
  2018-02-20  0:08 ` Vítor De Araújo
  0 siblings, 2 replies; 5+ messages in thread
From: Mark Carter @ 2018-02-18 21:56 UTC (permalink / raw)
  To: guile-user

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.




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Is this a good use for "compile"
  2018-02-18 21:56 Is this a good use for "compile" Mark Carter
@ 2018-02-19 17:30 ` Matt Wette
  2018-02-20  0:08 ` Vítor De Araújo
  1 sibling, 0 replies; 5+ messages in thread
From: Matt Wette @ 2018-02-19 17:30 UTC (permalink / raw)
  To: guile-user


On 02/18/2018 01:56 PM, 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

It is not clear to me how this will work.  First of all, compile may 
generate machine-dependent code.  Second, how do you propose to evaluate 
my-cell-formulae ?



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Is this a good use for "compile"
  2018-02-18 21:56 Is this a good use for "compile" Mark Carter
  2018-02-19 17:30 ` Matt Wette
@ 2018-02-20  0:08 ` Vítor De Araújo
  2018-02-20  9:28   ` Mark Carter
  1 sibling, 1 reply; 5+ messages in thread
From: Vítor De Araújo @ 2018-02-20  0:08 UTC (permalink / raw)
  To: Mark Carter, guile-user

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)
                                           total
                                         (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
https://elmord.org/



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Is this a good use for "compile"
  2018-02-20  0:08 ` Vítor De Araújo
@ 2018-02-20  9:28   ` Mark Carter
  0 siblings, 0 replies; 5+ messages in thread
From: Mark Carter @ 2018-02-20  9:28 UTC (permalink / raw)
  To: guile-user



On 20/02/18 00:08, Vítor De Araújo wrote:
>
> 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)
>                                           total
>                                         (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.
>
Thanks. A 10X speedup is just what the doctor ordered.

As it happened, I was looking for a way to time functions, and didn't 
realise that guile has a convenient way of doing this.



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Is this a good use for "compile"
@ 2018-02-20 11:03 Mark Carter
  0 siblings, 0 replies; 5+ messages in thread
From: Mark Carter @ 2018-02-20 11:03 UTC (permalink / raw)
  To: guile-user

: It is not clear to me how this will work.  First of all, compile may 
generate machine-dependent code.

That's OK. Generated code doesn't need to be saved. Formulae are 
compiled as needed.

: Second, how do you propose to evaluate my-cell-formulae ?

To evaluate a cell, I would call ((hash-ref my-cell-formula some-cell-ref))

Formulae are compiled to parameterless lambdas, which are stored in 
my-cell-formulae, So all I need to do is retrieve the relevant one, and 
execute it.




^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2018-02-20 11:03 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-02-18 21:56 Is this a good use for "compile" Mark Carter
2018-02-19 17:30 ` Matt Wette
2018-02-20  0:08 ` Vítor De Araújo
2018-02-20  9:28   ` Mark Carter
  -- strict thread matches above, loose matches on Subject: below --
2018-02-20 11:03 Mark Carter

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