Skip to content

Commit

Permalink
Improve performance
Browse files Browse the repository at this point in the history
Remove floatval() function from fromBytes() and toBytes() method to
improve performance in case that this class was called many times such
as uses in profiler.

Add more test to make sure that the test result cover all the byte
values.
  • Loading branch information
ve3 committed May 27, 2017
1 parent 8e8dc7c commit 31600ff
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Rundiz/Number/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function fromBytes($size, $unit = 'AUTO', $decimal = 2)
$byte_units = array_reverse($this->byte_units);
foreach ($byte_units as $size_unit => $size_unit_conversion) {
$size_unit_conversion = (string) $size_unit_conversion;
if (floatval($size) >= floatval($size_unit_conversion) && $unit == 'AUTO') {
if ($size >= $size_unit_conversion && $unit == 'AUTO') {
$output = bcdiv($size, $size_unit_conversion, $decimal).' '.$size_unit;
break;
}
Expand Down Expand Up @@ -166,7 +166,7 @@ public function toBytes($size)

$size_number = 0;
if (isset($matches[1])) {
$size_number = floatval($matches[1]);
$size_number = $matches[1];
} else {
return false;
}
Expand Down
11 changes: 11 additions & 0 deletions tests/phpunit/NumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,18 @@ public function testConvertByte()
{
$Number = new \Rundiz\Number\Number();
$this->assertEquals('100.00 GB', $Number->fromBytes(100000000000));
$this->assertEquals('123.45 GB', $Number->fromBytes(123456789123));
$this->assertEquals('123.45 B', $Number->fromBytes(123.456789123));
$this->assertFalse($Number->fromBytes(-123.456789123));

$this->assertEquals('8020000000000', $Number->toBytes('8.02TB'));
$this->assertEquals('123456789000', $Number->toBytes('123.456789GB'));
$this->assertEquals('98765', $Number->toBytes('98.765KB'));
$this->assertEquals('98765.43', $Number->toBytes('98.76543KB'));
$this->assertEquals('101135.80032', $Number->toBytes('98.76543KiB'));
$this->assertEquals('132560717806.04314', $Number->toBytes('123.456789GiB'));
$this->assertFalse($Number->toBytes('98.76543 KiB'));
$this->assertFalse($Number->toBytes('-123.456789GiB'));
}// testConvertByte


Expand Down

0 comments on commit 31600ff

Please sign in to comment.