Created
June 5, 2020 15:03
-
-
Save vyzo/3db10df3a1fd886bbda5d57369455ff6 to your computer and use it in GitHub Desktop.
A simple program to measure list accumulation performance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(import :std/srfi/1 | |
:std/misc/list | |
:gerbil/gambit/os) | |
(export main) | |
(def (accum1 lst) | |
(let lp ((rest lst) (r [])) | |
(match rest | |
([e . rest] | |
(lp rest (cons e r))) | |
(else | |
(reverse! r))))) | |
(def (accum2 lst) | |
(let (root [#f]) | |
(let lp ((rest lst) (tl root)) | |
(match rest | |
([e . rest] | |
(let (tl* [e]) | |
(set! (cdr tl) tl*) | |
(lp rest tl*))) | |
(else | |
(cdr root)))))) | |
(def (accum3 lst) | |
(with-list-builder (push) | |
(let lp ((rest lst)) | |
(match rest | |
([e . rest] | |
(push e) | |
(lp rest)) | |
(else (void)))))) | |
(def (main n) | |
(let* ((N (string->number n)) | |
(lst (iota N))) | |
(##gc) | |
(time (accum1 lst)) | |
(##gc) | |
(time (accum2 lst)) | |
(##gc) | |
(time (accum3 lst)))) |
It seems we had some very weird cpu caching effects; also gc minor faults!
I think the separate test restores sanity.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Variant to run only one variant at once, at which point the discrepancy between 3 and 6 becomes noise instead of being 4.5x. On the other hand, accum6 becomes 1 to 5% slower than accum2 instead of 2x faster. Whatever that means...