From: Volkan YAZICI
Subject: Re: Slow Loop (alternatives in lisp?)
Date: 
Message-ID: <6af77ad2-8e06-4d30-8b26-e2450c5f41dd@f36g2000hsa.googlegroups.com>
On Aug 16, 12:42 pm, Francogrex <······@grex.org> wrote:
>             (setf i 0)
>             (while (< i (length ls))
>               (setf j (+ i 1))
>               (while (< j (length ls))
>                 (if (AND (equal (nth i ls) (nth j ls)) (not (equal (nth j ls)
> 'indic)))
>                     (AND (setf (nth i counter)
>                                (+ (nth j counter) (nth i counter)))
>                          (AND (setf (nth j ls) 'indic) (setf (nth j counter) 'indic))))

I'm not an expert, but AFAIS for your code snippet, you're coding in C
using Common Lisp. I'd advise you to giving SICP[1] a try, and then
play with lisp again. Anyway, here is my 2 cents:

(defun concatenate-frequencies (items freqs &key (size 256) (test
'eql))
  (let ((uniq-freqs (make-hash-table :size size :test test)))
    (mapc
     (lambda (item freq)
       (setf (gethash item uniq-freqs)
             (+ (gethash item uniq-freqs 0) freq)))
     items
     freqs)
    (format t "~s~%" (hash-table-count uniq-freqs))
    (loop for item being each hash-key of uniq-freqs
          collect (cons item (gethash item uniq-freqs)))))

(let ((char-lo-limit #.(char-code #\a))
      (char-hi-limit #.(char-code #\z)))
  (defvar *items*
    (loop repeat 1000000
          with size = (- char-hi-limit char-lo-limit)
          collect (code-char (+ char-lo-limit (random size))))))

(defvar *freqs* (loop repeat 1000000 collect (random 1000)))

TEST> (time (concatenate-frequencies *items* *freqs* :size 1000000))
25
Evaluation
took:
  0.194 seconds of real
time
  0.192012 seconds of total run time (0.168010 user, 0.024002
system)
  [ Run times consist of 0.060 seconds GC time, and 0.133 seconds non-
GC
time. ]
  98.97%
CPU
  427,929,406 processor
cycles
  40,461,888 bytes
consed
((#\g . 20024018) (#\m . 19875326) (#\d . 20022273) (#\n .
19792959)
 (#\k . 20024116) (#\r . 19970136) (#\i . 19799098) (#\c .
20077641)
 (#\o . 19887064) (#\h . 19929361) (#\b . 20254118) (#\w .
19951612)
 (#\y . 20060516) (#\u . 20006662) (#\f . 19923075) (#\x .
19958120)
 (#\e . 19916717) (#\a . 19978439) (#\j . 20089621) (#\p .
20096731)
 (#\s . 19901291) (#\l . 20046440) (#\q . 19915451) (#\t .
19908266)
 (#\v . 20070813))

0.192 seconds for a list of 1,000,000 elements is not that bad.


Regards.

[1] http://mitpress.mit.edu/sicp/full-text/book/book.html