forked from hummypkg/webif
Optimise pretty_size function using log()
This commit is contained in:
parent
ebf7d1312e
commit
4ce3759902
@ -3,9 +3,19 @@ if {![exists -proc pretty_size]} {
|
|||||||
proc pretty_size {size} {
|
proc pretty_size {size} {
|
||||||
set units {bytes KiB MiB GiB TiB}
|
set units {bytes KiB MiB GiB TiB}
|
||||||
|
|
||||||
for {set i 0; set l [llength $units]; incr l -1} {
|
# for {set i 0; set l [llength $units]; incr l -1} {
|
||||||
$size > 1023 && $i < $l} {incr i} {
|
# $size > 1023 && $i < $l} {incr i} {
|
||||||
set size $($size / 1024.0)
|
# set size $($size / 1024.0)
|
||||||
|
# }
|
||||||
|
|
||||||
|
# We need to know how many powers of 1024 there are in
|
||||||
|
# size. This calculates the answer effeciently. Testing
|
||||||
|
# shows that this version takes 2/3 the time of the above.
|
||||||
|
if {$size == 0} {
|
||||||
|
set i 0
|
||||||
|
} else {
|
||||||
|
set i $(int(log($size) / log(1024)))
|
||||||
|
set size $($size / 1024.0 ** $i)
|
||||||
}
|
}
|
||||||
|
|
||||||
set size [string trimright \
|
set size [string trimright \
|
||||||
|
Loading…
Reference in New Issue
Block a user