From aac5240a0bc3456fe1085f2dd40a4818632ee485 Mon Sep 17 00:00:00 2001 From: Justin Ethier Date: Fri, 2 Jun 2017 19:25:26 -0400 Subject: [PATCH] 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. --- scheme/base.sld | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scheme/base.sld b/scheme/base.sld index c6839bb1..144d11b2 100644 --- a/scheme/base.sld +++ b/scheme/base.sld @@ -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)))))))