Benchmarking srfi-41, srfi-121 and srfi-158
Amirouche Boubekki 01 Aug 2020 14:29 UTC
% rlwrap guile -L .
GNU Guile 3.0.1
Copyright (C) 1995-2020 Free Software Foundation, Inc.
Guile comes with ABSOLUTELY NO WARRANTY; for details type `,show w'.
This program is free software, and you are welcome to redistribute it
under certain conditions; type `,show c' for details.
Enter `,help' for help.
xxxxxx@(guile-user)> (import (benchmark))
xxxxxx@(guile-user)> ,time (do-times (expt 2 8) (lambda () (sum/stream
(expt 2 16))))
$1 = 2147450880
;; 16.941247s real time, 36.446301s run time. 24.590046s spent in GC.
xxxxxx@(guile-user)> ,time (do-times (expt 2 8) (lambda ()
(sum/generator (expt 2 16))))
$2 = 2147450880
;; 0.941287s real time, 0.951202s run time. 0.013206s spent in GC.
xxxxxx@(guile-user)>
%
Here is the content of benchmark.scm:
(define-module (benchmark))
(use-modules (srfi srfi-41))
(use-modules (rnrs io ports))
(define-public (do-times n thunk)
(let loop ((n n)
(out 0))
(if (zero? n)
out
(loop (- n 1) (thunk)))))
(define (make-iota count start step)
(lambda ()
(cond
((<= count 0)
(eof-object))
(else
(let ((result start))
(set! count (- count 1))
(set! start (+ start step))
result)))))
(define (generator-fold f seed generator)
(let loop ((obj (generator))
(seed seed))
(if (eof-object? obj)
seed
(loop (generator) (f obj seed)))))
(define-public (sum/stream n)
(stream-fold + 0 (stream-range 0 n)))
(define-public (sum/generator n)
(generator-fold + 0 (make-iota n 0 1)))