Cool, never knew that existed.
$i = abs($i);
seems to be about 40% quicker than
$i *= -1;
however the fastest method (on average) seems to be
$i = 0 - $i;
Here is my code for testing that theory...
$tot1 = $tot2 = $tot3 = 0;
$loops = 10;
for ($i = 0; $i < $loops; $i++) {
$pos = -32.145263;
$start1 = microtime(TRUE);
$calc1 = $pos *= -1;
$end1 = microtime(TRUE);
$tot1 += ($end1 - $start1);
$start2 = microtime(TRUE);
$calc2 = abs($pos);
$end2 = microtime(TRUE);
$tot2 += ($end2 - $start2);
$start3 = microtime(TRUE);
$calc3 = 0 - $pos;
$end3 = microtime(TRUE);
$tot3 += ($end3 - $start3);
}
print ("Average totals:\n\$pos *= -1: ".($tot1 / $loops)."\nabs(\$pos): ".($tot2 / $loops)."\n0 - \$pos: ".($tot3 / $loops)."\n");
...and on my win32 machine that outputs
Average totals:
$pos *= -1: 4.9829483032227E-6
abs($pos): 3.4332275390625E-6
0 - $pos: 2.5272369384766E-6
abs() doesn't flip back to negative, which the other two do...