On 9/28/06, Horst H. [email protected] wrote:
index = ( Math.log( a_number ) / Math.log( 2 ) ).to_i / 10
def format_nice_bytes(n)
[[1073741824, “GB”], [1048576, “MB”], [1024, “KB”], [0, “B”]].each
{|
u| if (n > u[0]) then return printf(“%.2f %s”,n/u[0], u[1]); end }
end
I am sure somebody more familiar with Ruby will come up with a far more
elegant and efficient solution
Horst
–~—
I do sometimes get carried away with open ended solutions…
Running a benchmark on the code (original method is 1, your suggestion
is 2,
and a modified version of your suggestion is 3) gives
format_nice_bytes1 0.031000 0.000000 0.031000 ( 0.032000)
format_nice_bytes2 0.110000 0.031000 0.141000 ( 0.234000)
format_nice_bytes3 0.062000 0.000000 0.062000 ( 0.063000)
But this doesn’t really mean much. There are two main differences that
are
super apparent to me with this one.
- each run through methods 2 and 3 had to create the array of units for
every method call (very expensive)
- method 2 forced output to std out instead of just returning a string.
A refinement of taking the unit array out of the method into a constant
so
that it’s apples with apples on method 1 provides
format_nice_bytes1 0.031000 0.000000 0.031000 ( 0.031000)
format_nice_bytes2 0.110000 0.000000 0.110000 ( 0.234000)
format_nice_bytes3 0.047000 0.000000 0.047000 ( 0.047000)
format_nice_bytes4 0.031000 0.000000 0.031000 ( 0.031000)
There doesn’t look like there’s much in it between method 1 and 4, but 4
is
easier to read. I haven’t done many benchmarks though so I’d be more
than
happy to have other feedback.
I’ve attached the test file that I used and listed the methods below.
BYTE_UNITS = [“B”, “KB”, “MB”, “GB” ]
BYTE_UNITS2 =[[1073741824, “GB”], [1048576, “MB”], [1024, “KB”], [0,
“B”]]
def format_nice_bytes1( a_number )
index = ( Math.log( a_number ) / Math.log( 2 ) ).to_i / 10
“#{a_number.to_i / ( 1024 ** index ) } #{BYTE_UNITS[index]}”
end
def format_nice_bytes2(n)
[[1073741824, “GB”], [1048576, “MB”], [1024, “KB”], [0,
“B”]].each
{|u| if (n > u[0]) then return printf(“%.2f %s”,n/u[0], u[1]); end }
end
def format_nice_bytes3(n)
unit = [[1073741824, “GB”], [1048576, “MB”], [1024, “KB”], [0,
“B”]].detect{ |u| n > u[0] }
“#{n/unit[0]} #{unit[1]}”
end
def format_nice_bytes4(n)
unit = BYTE_UNITS2.detect{ |u| n > u[0] }
“#{n/unit[0]} #{unit[1]}”
end