unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: "Vítor De Araújo" <vbuaraujo@sapo.pt>
To: Mark Carter <alt.mcarter@gmail.com>, guile-user@gnu.org
Subject: Re: Is this a good use for "compile"
Date: Mon, 19 Feb 2018 21:08:25 -0300	[thread overview]
Message-ID: <5d3802ce-88b8-7b8a-13e5-b1fbd9dfd0c9@sapo.pt> (raw)
In-Reply-To: <d5f145f3-ce31-71e4-d256-229ec21d2668@gmail.com>

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/



  parent reply	other threads:[~2018-02-20  0:08 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2018-02-20  9:28   ` Mark Carter
  -- strict thread matches above, loose matches on Subject: below --
2018-02-20 11:03 Mark Carter

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=5d3802ce-88b8-7b8a-13e5-b1fbd9dfd0c9@sapo.pt \
    --to=vbuaraujo@sapo.pt \
    --cc=alt.mcarter@gmail.com \
    --cc=guile-user@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).