optimizing list processing loop with explode

Notices and updates
Locked
Tim Johnson
Posts: 253
Joined: Thu Oct 07, 2004 7:21 pm
Location: Palmer Alaska USA

optimizing list processing loop with explode

Post 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

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post 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.

Tim Johnson
Posts: 253
Joined: Thu Oct 07, 2004 7:21 pm
Location: Palmer Alaska USA

Post 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

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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

Tim Johnson
Posts: 253
Joined: Thu Oct 07, 2004 7:21 pm
Location: Palmer Alaska USA

Post 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

Locked