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

Avoiding consing when translating numbers to strings

If you use the optional second argument to TIME you'll see that FORMAT
is consing a bunch of bignums.  In order to avoid using floating point
arithmetic FORMAT uses things like INTEGER-DECODE-FLOAT to convert the
float to a set of integers it can process.  For some floats this can
result in a bignum.  Much of the arithmetic that then takes place to
convert this to a string conses more bignums.  In the more common cases,
though, only fixnums are produced, so the integer arithmetic ends up
being much faster than it would have been had it been done with the
original floats; unfortunately, this idea loses if the integers are

One thing that I noticed reduced the consing a bit (examine-float
1d-100) went from 126 to 70 words consed) was to specify the width
option to ~F, e.g. ~10F.

The functions to look at for most of the consing are
~nF).  To do much better than these, I think you'll probably have to
implement stack-consed bignums in some way (probably making use of the
undocumented %BIGNUM-xxx subprimitives).  You could probably reduce the
consing by using floating point arithmetic (it conses, but not as much),
but it makes up for the reduced consing by being slower than integer