Skip to content

Commit

Permalink
Improve performance of multipliedBy one if value is one (#24)
Browse files Browse the repository at this point in the history
* Improve performance of multipliedBy one if value is one

* Improve performance of zero minus an number

* Improve performance of zero plus any number

* Improve performance when zero gets divided by any number

* Improve performance of zero minus any number

* Improve performance of zero plus any number

* Improve performance of one multiplied to any number

* Improve performance of zero multiplied by any number

* Improve performance of zero multiplied by any number

* Improve code coverage.

* Remove over the top optimizations
  • Loading branch information
tomtomsen authored and BenMorel committed Jan 8, 2020
1 parent e793795 commit 2667c16
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/BigDecimal.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ public function plus($that) : BigDecimal
return $this;
}

if ($this->value === '0' && $this->scale <= $that->scale) {
return $that;
}

$this->scaleValues($this, $that, $a, $b);

$value = Calculator::get()->add($a, $b);
Expand Down Expand Up @@ -202,6 +206,10 @@ public function multipliedBy($that) : BigDecimal
return $this;
}

if ($this->value === '1' && $this->scale === 0) {
return $that;
}

$value = Calculator::get()->mul($this->value, $that->value);
$scale = $this->scale + $that->scale;

Expand Down
8 changes: 8 additions & 0 deletions src/BigInteger.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ public function plus($that) : BigInteger
return $this;
}

if ($this->value === '0') {
return $that;
}

$value = Calculator::get()->add($this->value, $that->value);

return new BigInteger($value);
Expand Down Expand Up @@ -269,6 +273,10 @@ public function multipliedBy($that) : BigInteger
return $this;
}

if ($this->value === '1') {
return $that;
}

$value = Calculator::get()->mul($this->value, $that->value);

return new BigInteger($value);
Expand Down
53 changes: 53 additions & 0 deletions tests/BigDecimalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,16 @@ public function providerPlus()
['123.00', '999.0', '112200', 2],
['123.00', '999.00', '112200', 2],

['0', '999', '999', 0],
['0', '999.0', '9990', 1],
['0', '999.00', '99900', 2],
['0.0', '999', '9990', 1],
['0.0', '999.0', '9990', 1],
['0.0', '999.00', '99900', 2],
['0.00', '999', '99900', 2],
['0.00', '999.0', '99900', 2],
['0.00', '999.00', '99900', 2],

['123', '-999', '-876', 0],
['123', '-999.0', '-8760', 1],
['123', '-999.00', '-87600', 2],
Expand Down Expand Up @@ -569,6 +579,8 @@ public function providerMinus()
['123.00', '999', '-87600', 2],
['123.00', '999.0', '-87600', 2],
['123.00', '999.00', '-87600', 2],
['0', '999', '-999', 0],
['0', '999.0', '-9990', 1],

['123', '-999', '1122', 0],
['123', '-999.0', '11220', 1],
Expand Down Expand Up @@ -606,6 +618,7 @@ public function providerMinus()
['-2348783784774335.3232342791', '-309049304233536.556172', '-20397344805407987670622791', 10],

['1234568798347983.2334899238921', '0', '12345687983479832334899238921', 13],
['0', '1234568798347983.2334899238921', '-12345687983479832334899238921', 13],
['-0.00223287647368738736428467863784', '0.000', '-223287647368738736428467863784', 32],
];
}
Expand Down Expand Up @@ -674,6 +687,46 @@ public function providerMultipliedBy()
['-123.00', '-999.0', '122877000', 3],
['-123.00', '-999.00', '1228770000', 4],

['1', '999', '999', 0],
['1', '999.0', '9990', 1],
['1', '999.00', '99900', 2],
['1.0', '999', '9990', 1],
['1.0', '999.0', '99900', 2],
['1.0', '999.00', '999000', 3],
['1.00', '999', '99900', 2],
['1.00', '999.0', '999000', 3],
['1.00', '999.00', '9990000', 4],

['123', '1', '123', 0],
['123', '1.0', '1230', 1],
['123', '1.00', '12300', 2],
['123.0', '1', '1230', 1],
['123.0', '1.0', '12300', 2],
['123.0', '1.00', '123000', 3],
['123.00', '1', '12300', 2],
['123.00', '1.0', '123000', 3],
['123.00', '1.00', '1230000', 4],

['0', '999', '0', 0],
['0', '999.0', '0', 1],
['0', '999.00', '0', 2],
['0.0', '999', '0', 1],
['0.0', '999.0', '0', 2],
['0.0', '999.00', '0', 3],
['0.00', '999', '0', 2],
['0.00', '999.0', '0', 3],
['0.00', '999.00', '0', 4],

['123', '0', '0', 0],
['123', '0.0', '0', 1],
['123', '0.00', '0', 2],
['123.0', '0', '0', 1],
['123.0', '0.0', '0', 2],
['123.0', '0.00', '0', 3],
['123.00', '0', '0', 2],
['123.00', '0.0', '0', 3],
['123.00', '0.00', '0', 4],

['589252.156111130', '999.2563989942545241223454', '5888139876152080735720775399923986443020', 31],
['-589252.15611130', '999.256398994254524122354', '-58881398761537794715991163083004200020', 29],
['589252.1561113', '-99.256398994254524122354', '-584870471152079471599116308300420002', 28],
Expand Down
7 changes: 7 additions & 0 deletions tests/BigIntegerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,8 @@ public function providerPlus()
return [
['5165450198704521651351654564564089798441', '0', '5165450198704521651351654564564089798441'],
['-5165450198704521651351654564564089798441', '0', '-5165450198704521651351654564564089798441'],
['0', '5165450198704521651351654564564089798441', '5165450198704521651351654564564089798441'],
['0', '-5165450198704521651351654564564089798441', '-5165450198704521651351654564564089798441'],
['5165450198704521651351654564564089798441', '-5165450198704521651351654564564089798441', '0'],
['-5165450198704521651351654564564089798441', '5165450198704521651351654564564089798441', '0'],

Expand Down Expand Up @@ -586,10 +588,14 @@ public function providerMultipliedBy()
return [
['123456789098765432101234567890987654321', '1', '123456789098765432101234567890987654321'],
['123456789098765432101234567890987654321', '-1', '-123456789098765432101234567890987654321'],
['1', '123456789098765432101234567890987654321', '123456789098765432101234567890987654321'],
['15892588375910581333', '2485910409339228962451', '39507550875019745254366764864945838527183'],
['341581435989834012309', '-91050393818389238433', '-31101124267925302088072082300643257871797'],
['-1204902920503999920003', '1984389583950290232332', '-2390996805119422027350037939263960284136996'],
['-991230349304902390122', '-3483910549230593053437', '3453357870660875087266990729629471366949314'],
['0', '-3483910549230593053437', '0'],
['-991230349304902390122', '0', '0'],


['1274837942798479387498237897498734984', 30, '38245138283954381624947136924962049520'],
['1274837942798479387498237897498734984', 30.0, '38245138283954381624947136924962049520'],
Expand Down Expand Up @@ -639,6 +645,7 @@ public function providerDividedBy()
['123456789098765432101234567890987654322', '14/7', '61728394549382716050617283945493827161'],
['61728394549382716050617283945493827161', '0.5', RoundingNecessaryException::class],
['61728394549382716050617283945493827161', '1/2', RoundingNecessaryException::class],
['0', '61728394549382716050617283945493827161', '0'],

// overflowing native integer division
['-2147483648', '-1', '2147483648'], // 32-bit
Expand Down
3 changes: 3 additions & 0 deletions tests/Internal/Calculator/NativeCalculatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public function providerMul()
return [
['0', '0', '0'],

['0', '1234567891234567889999999', '0'],
['1234567891234567889999999', '0', '0'],

['1', '1234567891234567889999999', '1234567891234567889999999'],
['1234567891234567889999999', '1', '1234567891234567889999999'],

Expand Down

0 comments on commit 2667c16

Please sign in to comment.