Howdy Folks.
given the following:
(set 'l '( 1 2 3 4 5 6 7 8))
;; I'd like to process 'l two elements at a time:
;; which is more efficient?
;; 1)
(dolist (x (explode l 2)) (code-here x))
;; 2)
(set 'l1 (explode l 2))
(dolist (x l1) (code-here x)
;; OR is there a more efficient way than using 'explode?
;; Also, any links to similar topics regarding loop
;; optimization would be warmly welcomed
Cheers
Tim
Hi Tim! The classic way of testing speed is using 'time':
(set 'l (sequence 100 900))
(define (code-here pair)
(reverse pair))
(println "1n"
(time
(dolist (x (explode l 2))
(code-here x)) 2000))
(println "2n"
(time
(begin
(set 'l1 (explode l 2))
(dolist (x l1)
(code-here x))) 2000))
(set 'l1 (explode l 2))
(println "3n"
(time
(begin
(dolist (x l1)
(code-here x))) 2000))
1
582
2
673
3
529
where you adjust the 2000 repetitions to match your situation and patience. But there are subtleties to timing and benchmarking which I might be missing! 3 is slightly quicker obviously because the explosion itself isn't timed...
I've looked into other ways of timing stuff, and described my thoughts and experiments on my blog. Look at //http://unbalanced-parentheses.nfshost.com/index.cgi?view-post-id=20060514071900 for an attempt at profiling using macros. Fanda's typically elegant solution is reproduced here //http://unbalanced-parentheses.nfshost.com/index.cgi?view-post-id=20060514071900. Also, processing list elements in pairs got some treatment here //http://unbalanced-parentheses.nfshost.com/index.cgi?view-post-id=20071206122757.
Looks like calling 'explode from the dolist loop is _not_ a
performance breaker.
Hey thanks for the demo and the links. You've given me a
lot of good stuff to digest.
cheers
Tim
Quote
Looks like calling 'explode from the dolist loop is _not_ a
performance breaker.
In general, nesting expressions will increase performance, because data flows from one expression directly into the calling one. In your case the result from 'explode' is immediately used by 'dolist'.
When you want performance: the more you can nest expressions, the better. The only reason you wouldn't do it, is readability and maintainability of the source code.
Lutz
Understood.
It's great to have a developer who is so actively
engaged with the community and so revealing about the inner
workings.
Best wishes to all for 2008.
Tim