Skip to content
This repository has been archived by the owner on Feb 24, 2020. It is now read-only.

Commit

Permalink
Optimise pretty_size function using log()
Browse files Browse the repository at this point in the history
  • Loading branch information
hummypkg committed Aug 31, 2017
1 parent ebf7d13 commit 4ce3759
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions webif/lib/pretty_size
Expand Up @@ -3,9 +3,19 @@ if {![exists -proc pretty_size]} {
proc pretty_size {size} {
set units {bytes KiB MiB GiB TiB}

for {set i 0; set l [llength $units]; incr l -1} {
$size > 1023 && $i < $l} {incr i} {
set size $($size / 1024.0)
# for {set i 0; set l [llength $units]; incr l -1} {
# $size > 1023 && $i < $l} {incr i} {
# 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 \
Expand Down

0 comments on commit 4ce3759

Please sign in to comment.