From: Pascal J. Bourguignon
Subject: Re: Slow Loop (alternatives in lisp?)
Date: 
Message-ID: <87iqu0nb9a.fsf@hubble.informatimago.com>
Francogrex <······@grex.org> writes:

> Hello, I'm trying to imitate the behaviour of the pivot-table in excel
> where you take a list of items and another list of their values and
> you sum similar ones together (see toy example below). I have a list
> of 30000 items and their associated values and in excel using a pivot-
> table the computation is done instantaneously (less than 2 seconds)
> while the procedure I wrote in lisp will take about 12 hours !(I give
> an example of only 15 items below, this goes fast of course because
> only 15 items, but the 30,000 will take an estimate of about 12 hours;
> I never reached that far because around 5 hours I give up). Do you
> know why? Is there a way to enhance the procedure and make it as fast
> as the pivot table? Thanks


;; Tabulate like the pivot table.
(time
 (let ((ls      (vector "a" "b" "c" "b" "a" "f" "e" "g"
                        "h" "k" "z" "k" "r" "u" "f")) 
       (counter (vector 1 5 8 7 14 8 3 7 9 4 3 21 5 7 9))
       (i 0))
   (loop while (< i (length ls)) do
        (let ((j (+ i 1)))
          (loop while (< j (length ls)) do
               (when (and (equal (elt ls i) (elt ls j))
                          (not (equal (elt ls j) 'indic)))
                 (incf (elt counter i) (elt counter j))
                 (setf (elt ls      j) 'indic
                       (elt counter j) 'indic))
               (incf j)))
        (incf i))
   (values (delete 'indic ls)
           (delete 'indic counter))))

Real time: 0.009765 sec.
Run time: 0.012 sec.
Space: 102408 Bytes
#("a" "b" "c" "f" "e" "g" "h" "k" "z" "r" "u") ;
#(15 12 8 17 3 7 9 25 3 5 7)



-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

"You question the worthiness of my code? I should kill you where you
stand!"