3 ms·
It depends where you come from. If you started with the typical C-influenced programming languages, sure, the last one is not only obviously the fastest (edit:
by onli 12y ago
It depends where you come from. If you started with the typical C-influenced programming languages, sure, the last one is not only obviously the fastest (edit: on a normal computer), it is also the first one you would write. But things are different if you are used to write in functional lisp-like languages, and one can clearly see that he is coming from that direction (using recursion as first try, knowing reduce, …).
- thegeomaster 12y agoBut a Lisper would do something along the lines of (defun list-sum (lyst &optional (acc 0)) (if (eql 0 (length lyst)) acc (list-sum (cdr lyst) (+ acc (car lyst))))) And any respectable compiler (with TCO) would compile it to a plain old loop. In this case, I'm puzzled because the author implemented this solution knowing that it is going to be compiled to a slow recursive routine.
- adwf 12y agoI'd hope they'd also first think of: (reduce #'+ list-of-numbers) Then using a library like lparallel [1], you can convert it to the parallel version easily: (preduce '+ list-of-numbers) [1] http://lparallel.org/preduce/ http://lparallel.org/preduce/