unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Andy Wingo <wingo@pobox.com>
To: guile-user@gnu.org
Subject: Re: Playing with guile (vs python). Generate file for GDP suitable for gnuplot.
Date: Mon, 06 Mar 2017 22:02:25 +0100	[thread overview]
Message-ID: <878toi9l0e.fsf@pobox.com> (raw)
In-Reply-To: <878toi5xaw.fsf@zigzag.favinet> (Thien-Thi Nguyen's message of "Mon, 06 Mar 2017 14:50:31 +0100")

On Mon 06 Mar 2017 14:50, Thien-Thi Nguyen <ttn@gnu.org> writes:

> FWIW, we can also use SRFI 13 to save a few cycles:
>
>  (use-modules (ice-9 rdelim) (srfi srfi-13))
>  
>  (define (file->lines filename)
>    "Return a list of lines contained in a file."
>    (call-with-input-file
>        filename
>      (lambda (p)
>        (let loop ((acc '()))
>          (let ((line (read-line p)))
>            (if (eof-object? line)
>                (reverse! acc)            ; rv
>                (loop (cons (string-drop-right line 1)
>                            acc))))))))
>
> This variant is also more stack-conserving (amenable to TCO),
> which should afford some additional speedup.

Interestingly the timings are not dissimilar in 2.2; consider this most
microbenchmarky of cases:

    (use-modules (ice-9 match))
    (define test (iota #e1e6))
    (define (copy-list/accum xs)
      (let lp ((xs xs) (out '()))
        (match xs
          ((x . xs) (lp xs (cons x out)))
          (() (reverse! out)))))
    (define (copy-list/recur xs)
      (let lp ((xs xs))
        (match xs
          ((x . xs) (cons x (lp xs)))
          (() '()))))

Doing a ,time (length (copy-list test) at the REPL for both these two
copy-list impls gives me:

                 real times (s)
    ------------------------------------
    copy-list/recur   |  copy-list/accum
    ....................................
    0.067212s           0.046110s
    0.077659s           0.059750s
    0.065923s           0.040371s
    0.066098s           0.062963s
    0.062718s           0.040980s

The recursive version is still slower ATM but I think that will go away
once I manage to avoid running the function prelude each time; not
sure.  Anyway, I am happy recommending recursing now :)

Andy



      reply	other threads:[~2017-03-06 21:02 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-31  5:19 Playing with guile (vs python). Generate file for GDP suitable for gnuplot Germán Diago
2017-01-31  7:40 ` Alex Vong
2017-01-31  9:41 ` Arne Babenhauserheide
2017-02-01  3:56   ` Germán Diago
2017-02-01 22:24     ` Arne Babenhauserheide
2017-02-02  8:32       ` Germán Diago
2017-02-11 14:12 ` Ludovic Courtès
2017-03-06 13:50   ` Thien-Thi Nguyen
2017-03-06 21:02     ` Andy Wingo [this message]

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=878toi9l0e.fsf@pobox.com \
    --to=wingo@pobox.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).