[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Playing with guile (vs python). Generate file for GDP suitable for g
From: |
Andy Wingo |
Subject: |
Re: Playing with guile (vs python). Generate file for GDP suitable for gnuplot. |
Date: |
Mon, 06 Mar 2017 22:02:25 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/25.1 (gnu/linux) |
On Mon 06 Mar 2017 14:50, Thien-Thi Nguyen <address@hidden> 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