PHP pow(n,2) or (n*n) ?

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

I ran across someone optimizing some code a couple of weeks ago, and one of the things the person answering their question did was replace calls to pow(n,2) with (n*n) since apparently, the later is faster. The language in question wasn't PHP though, it was some form of C they were optimizing.

I've got a situation were using pow() allows me to calculate the base a single time without storing it in a variable, but I'm wondering if I should just go ahead and use that variable since it's only a power of 2, or even do the calculation twice.

PHP Code: [ Select ]
$w = pow(abs($left - $right), 2);


PHP Code: [ Select ]
$w = abs($left - $right);
$w *= $w;
  1. $w = abs($left - $right);
  2. $w *= $w;


PHP Code: [ Select ]
$w = abs($left - $right) * abs($left - $right);
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

Looks like I have my answer! :)

PHP Code: [ Select ]
<?php
 
$left = 100;
$right = 225;
$times = array(0.0, 0.0, 0.0);
 
$start = microtime(true);
for($i = 1000000; --$i; )
{
    $w = pow(abs($left - $right), 2);
}
$times[0] = microtime(true) - $start;
 
 
 
$start = microtime(true);
for($i = 1000000; --$i; )
{
    $w = abs($left - $right);
    $w *= $w;
}
$times[1] = microtime(true) - $start;
 
 
 
$start = microtime(true);
for($i = 1000000; --$i; )
{
    $w = abs($left - $right) * abs($left - $right);
}
$times[2] = microtime(true) - $start;
 
echo '<pre>', print_r($times, true), '</pre>';
/*
Array
(
    [0] => 3.7
    [1] => 2.176
    [2] => 3.246
)
*/
 
?>
  1. <?php
  2.  
  3. $left = 100;
  4. $right = 225;
  5. $times = array(0.0, 0.0, 0.0);
  6.  
  7. $start = microtime(true);
  8. for($i = 1000000; --$i; )
  9. {
  10.     $w = pow(abs($left - $right), 2);
  11. }
  12. $times[0] = microtime(true) - $start;
  13.  
  14.  
  15.  
  16. $start = microtime(true);
  17. for($i = 1000000; --$i; )
  18. {
  19.     $w = abs($left - $right);
  20.     $w *= $w;
  21. }
  22. $times[1] = microtime(true) - $start;
  23.  
  24.  
  25.  
  26. $start = microtime(true);
  27. for($i = 1000000; --$i; )
  28. {
  29.     $w = abs($left - $right) * abs($left - $right);
  30. }
  31. $times[2] = microtime(true) - $start;
  32.  
  33. echo '<pre>', print_r($times, true), '</pre>';
  34. /*
  35. Array
  36. (
  37.     [0] => 3.7
  38.     [1] => 2.176
  39.     [2] => 3.246
  40. )
  41. */
  42.  
  43. ?>
  • IcyDragoon
  • Student
  • Student
  • IcyDragoon
  • Posts: 65

Post 3+ Months Ago

Great Finding!

You should create a tutorial on optimizing PHP codes. ^^
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

I think it's interesting that it was actually faster to calculate the base twice than it was to use pow(). I wonder why it worked like that.

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 123 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.