What value should a Tcl dict be given for minimal memory?

I need a dictionary, "only for keys", that is, values ​​have a smaller meaning. What value will consume the least amount of memory? "0" "", something else? Thank.

+4
source share
2 answers

Tcl supports constants under covers, so you can use almost anything while it is literal. But an empty line will almost certainly be a predefined constant in your script anyway (even if you don't notice it) and is rather short. Or go with a single-character alphanumeric string that will generate a shorter lowercase dictionary form (and makes almost no difference). A 0is a single-character alphanumeric string, of course.

In my own code, I would mostly use something like "dummy value"for value. The cost of just a few bytes is greater in most cases, and yet it is much clearer to me that it does not mean anything, so if I return to the code later, I will not try to figure out what I am doing with the value ...

+6

, , , ...

, , .

, :

for {set i 0} {$i < 1000000} {incr i} {
    set idx [expr rand()]
    incr arr($idx)
    dict incr dic $idx
}
set rands [dict keys $dic]

, 5,88 , set rands....

lsearch, dict exists info exists arr() 1000 :

lsearch $rands [expr rand()]      26349.115 microseconds per iteration
dict exists $dic [expr rand ()]      14.357 microseconds per iteration
info exists arr([expr rand()])       14.652 microseconds per iteration

. , .

OP, , lsearch -sorted. .

lsearch -sorted $sortedRands [expr rand ()]   19.304 microseconds per iteration

, lsearch , , .

, -sorted,

lsearch $sortedRands [expr rand()]     120206.369 microseconds per iteration
                                       123604.209 microseconds per iteration

, . info exists arr(... , , , .

- , ?

+1

All Articles