Pure numbers

FW: A pure number is a numeric value for which Tcl has not yet generated a sring representation.

For performance, the string representation of numeric values computed by expr or other commands is not generated immediately, but instead delayed until it is actually needed. The string representation of a numeric value can be cleared with this trick:

incr x 0

Although it isn't strictly necessary for Tcl to clear the string representation in this case, for some reason it does.

The use for pure numeric values doesn't come up as often as the use for other pure values such as lists and dictionaries, since the strin representation of a number is usually relatively small.

Donal Fellows, whose explanations were mighty helpful in the creation of this page, points out, however, that if you take into account Tcl_Obj overhead, the most a string representation could add - for an integer, at least - is a 2x gain in memory profile.


DKF - Note that there's not much point in worrying whether a number is pure in Tcl or not. Just use the things. Where you can gain is when you are storing loads of instances of the same value (e.g. a list of zeros, ones and twos) and the way in which you can gain there is by using the same '0', '1' or '2' for each list member, instead of packing in the value you computed. Perhaps like this:

# Version that uses *lots* of memory
set mainlist {}
for {set i 0} {$i<1000000} {incr i} {
    lappend mainlist [expr {$i%3}]
}

# Version that uses much less...
set cache {0 1 2}
set mainlist {}
for {set i 0} {$i<1000000} {incr i} {
    lappend mainlist [lindex $cache [expr {$i%3}]]
}

Tcl uses tricks like this internally when you use binary split to try to minimise the number of objects generated...

Lars H: Those that worry about this kind of memory usage issues may want to take a look at (and possibly contribute to) the Compact Data Storage page.


RS 2004-09-22: Just today I learnt that Python has a pool of small integers (possibly 0..9) that are shared between uses. Would that be helpful in Tcl too? Those numbers are used most often, and using them might make dkf's above version 1 as efficient as version 2, without explicit caching...


RS As long as they don't get optimized out, here's two ways of producing a pure number, integer or float:

expr {$x+0}
expr {$x*1}

Other Page Authors

PYK