Page 1 of 1
					
				optimizing list processing loop with explode
				Posted: Mon Dec 31, 2007 2:06 am
				by Tim Johnson
				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)
Code: Select all
(dolist (x (explode l 2)) (code-here x))
;; 2)
Code: Select all
(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
 
			 
			
					
				
				Posted: Mon Dec 31, 2007 9:28 am
				by cormullion
				Hi Tim! The classic way of testing speed is using 'time':
Code: Select all
(set 'l (sequence 100 900))
(define (code-here pair)
    (reverse pair))
(println "1\n"
    (time
        (dolist (x (explode l 2))
            (code-here x)) 2000))
(println "2\n"
    (time
        (begin
            (set 'l1 (explode l 2))
            (dolist (x l1)
                (code-here x))) 2000))
(set 'l1 (explode l 2))
(println "3\n"
    (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.c ... 0514071900 for an attempt at profiling using macros. Fanda's typically elegant solution is reproduced here 
http://unbalanced-parentheses.nfshost.c ... 0514071900. Also, processing list elements in pairs got some treatment here 
http://unbalanced-parentheses.nfshost.c ... 1206122757.
 
			 
			
					
				
				Posted: Mon Dec 31, 2007 4:49 pm
				by Tim Johnson
				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
			 
			
					
				
				Posted: Mon Dec 31, 2007 5:43 pm
				by Lutz
				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
 
			 
			
					
				
				Posted: Mon Dec 31, 2007 6:17 pm
				by Tim Johnson
				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