From: Thomas A. Russ
Subject: Re: Lame code needs improvement
Date: 
Message-ID: <ymi3cjnoq5q.fsf@sevak.isi.edu>
Joe Marshall <···@ccs.neu.edu> writes:
> "Smeckler" <······················@hotmail.com> writes:
> > - I'm sure there must be a nicer way of adding characters to a list.
> 
> >   (setf num-list (append num-list (list (read-char stream nil))))))
> 
> NEVER do this!  It creates an O(n^2) process in the number of
> characters.  Every time through the loop you make an entirely new copy
> of all the characters you have read so far.
[snip]
> > - It seems wasteful to read the sequence into a list to find the size, then
> >   allocate a string and copy it.
> 
> If you are really that concerned about space, read the characters into
> a static buffer and then cons the string.

Some useful techniques if you want to be doing string processing and
reading your own characters for parsing is to make use of adjustable
arrays and fill pointers:

(defvar *buffer* (make-array 1024
                     :adjustable t :fill-pointer 0
                     :element-type 'character))

Then in your code you can reset the buffer with

(setf (fill-pointer *buffer*) 0)

add new elements to the end with the following function that will even
take care of increasing the buffer size if necessary (thanks to using an
adjustable array):

(vector-push-extend (read-char stream) *buffer*)

and then have the resulting string always be in *buffer*.  If you have a
single delimiter character, there is also the READ-DELIMITED-LIST
function that may simplify life for you.

Or you do just do this:

> ---
> You might want to consider accumulating the numeric token as a number.



-- 
Thomas A. Russ,  USC/Information Sciences Institute          ···@isi.edu