[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Avoiding consing when translating numbers to strings

	I have a question that I'd like the slug-wizards (?) to consider.
What I have is a function that needs to make a string representation of
a number, and examine that string, returning (say) a count; that is, it
doesn't need to return the string.  The function will be called extrememly
frequently, and so I'd like it not to use up any memory if possible.
	I have the following code fragment:

(defun examine-number (num)
  ;;create temporary storage to hold the string rep
  (sys:with-stack-array (string-num 100 :element-type 'string-char
                                        :fill-pointer 0)
    ;;fill the string with the number
    (with-output-to-string (stream string-num) (format stream "~F" num))
    ;;then deal with the string
    ;;currently nothing is done

	Why I'm asking is that (time)'ing this code indicates that it creates
nothing when it's run with integers, and SOME regular floats.  However, with
floats that are very small or double-precision, I'm told that
'xx structure words consed in WORKING-STORAGE-AREA'.  Usually this is a small
number (like 30) but for very small double-precision numbers, it can run in the
150 range.
	So, what am I doing wrong?  I'd just as soon not write my own
string-to-number routine if possible (the consing seems to happen in the
call to format).
	thanks in advance,
	chris sterritt