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

Avoiding consing when translating numbers to strings



FYI - you might want to try the following approach:

(defparameter *temp-string*
	      (make-array 10 :element-type 'string-char :fill-pointer 0))

(defun examine-number (num)
  ;; 1Protect the global parameter - format can set its first argument
0  (let ((outstr *temp-string*))
     (setf (fill-pointer outstr) 0)
     (format outstr "~F" num)
     ;; 1then deal with the string (in outstr)
0     ))

This won't get you away from the bignum consing Barry mentioned, but it avoids
the need to stack-allocate a new string each time, minimizing the overhead of
building the strings.  Probably will speed things up a bit.  You have to be 
careful because of format's modifying the global's contents in place.  I like 
to use this approach for short-lived things such as mouse documentation strings.