mirror of
https://github.com/justinethier/cyclone.git
synced 2025-05-23 20:15:05 +02:00
Avoid calling length each iteration of for-each
length is O(n) so calling it should be avoided, especially for each iteration of for-each. Instead the code can just check if the first cdr is null. This has the potential for a huge speed improvement.
This commit is contained in:
parent
5390802816
commit
aac5240a0b
1 changed files with 2 additions and 1 deletions
|
@ -764,7 +764,8 @@
|
|||
(apply f cars)
|
||||
(recur cdrs)))))
|
||||
;; Fast path.
|
||||
(if (eq? 1 (length lis1))
|
||||
;(if (eq? 1 (length lis1))
|
||||
(if (null? (cdr lis1)) ;; O(1) instead of O(n) for length
|
||||
(f (car lis1))
|
||||
(begin (f (car lis1))
|
||||
(for-each f (cdr lis1)))))))
|
||||
|
|
Loading…
Add table
Reference in a new issue