(N % 2) or (N & 1) for Odds and Evens

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

Post 3+ Months Ago

The results of this little test didn't turn out the way I thought they would. They're close, but one is consistently beating the other for me.

PHP Code: [ Select ]
<pre><?php
 
$total_1 = 0;
$total_2 = 0;
$total_3 = 0;
$i = 0;
$j = 0;
$k = 0;
$start_1 = 0;
$start_2 = 0;
$start_3 = 0;
$end_1 = 0;
$end_2 = 0;
$end_3 = 0;
 
$start_1 = microtime(true);
for($i = 0; $i < 1000000; $i++)
{
   if ($i % 2)
   {
      $total_1++;
   }
}
$end_1 = microtime(true);
 
$start_2= microtime(true);
for($j = 0; $j < 1000000; $j++)
{
   if ($j & 1)
   {
      $total_2++;
   }
}
$end_2 = microtime(true);
 
$start_3 = microtime(true);
for($k = 0; $k < 1000000; $k++)
{
   if ($k << 31)
   {
      $total_3++;
   }
}
$end_3 = microtime(true);
 
echo '$i % 2: ', ($end_1 - $start_1), " | total: $total_1\n";
echo '$j & 1: ', ($end_2 - $start_2), " | total: $total_2\n";
echo '$k << 31: ', ($end_3 - $start_3), " | total: $total_3\n";
 
?></pre>
  1. <pre><?php
  2.  
  3. $total_1 = 0;
  4. $total_2 = 0;
  5. $total_3 = 0;
  6. $i = 0;
  7. $j = 0;
  8. $k = 0;
  9. $start_1 = 0;
  10. $start_2 = 0;
  11. $start_3 = 0;
  12. $end_1 = 0;
  13. $end_2 = 0;
  14. $end_3 = 0;
  15.  
  16. $start_1 = microtime(true);
  17. for($i = 0; $i < 1000000; $i++)
  18. {
  19.    if ($i % 2)
  20.    {
  21.       $total_1++;
  22.    }
  23. }
  24. $end_1 = microtime(true);
  25.  
  26. $start_2= microtime(true);
  27. for($j = 0; $j < 1000000; $j++)
  28. {
  29.    if ($j & 1)
  30.    {
  31.       $total_2++;
  32.    }
  33. }
  34. $end_2 = microtime(true);
  35.  
  36. $start_3 = microtime(true);
  37. for($k = 0; $k < 1000000; $k++)
  38. {
  39.    if ($k << 31)
  40.    {
  41.       $total_3++;
  42.    }
  43. }
  44. $end_3 = microtime(true);
  45.  
  46. echo '$i % 2: ', ($end_1 - $start_1), " | total: $total_1\n";
  47. echo '$j & 1: ', ($end_2 - $start_2), " | total: $total_2\n";
  48. echo '$k << 31: ', ($end_3 - $start_3), " | total: $total_3\n";
  49.  
  50. ?></pre>
  • 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

Out of curiosity I tried with bit shifting. Bit shifting works pretty much on par with modulo, they both beat eachother about half the time. The bitwise AND is consistiently slower than both modulo and bit shifting.

I updated the cod in my original post.
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6230
  • Loc: South-Africa

Post 3+ Months Ago

on my wamp the script wrote:
$i % 2: 0.27078700065613 | total: 500000
$j & 1: 0.24281907081604 | total: 500000
$k << 31: 0.25018215179443 | total: 500000
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

My first run this afternoon.

Code: [ Select ]
$i % 2: 0.2589 | total: 500000
$j & 1: 0.2662 | total: 500000
$k << 31: 0.2493 | total: 500000
  1. $i % 2: 0.2589 | total: 500000
  2. $j & 1: 0.2662 | total: 500000
  3. $k << 31: 0.2493 | total: 500000


After a few runs it went back to looking like yesterday though.
Code: [ Select ]
$i % 2: 0.2567 | total: 500000
$j & 1: 0.272 | total: 500000
$k << 31: 0.25 | total: 500000
  1. $i % 2: 0.2567 | total: 500000
  2. $j & 1: 0.272 | total: 500000
  3. $k << 31: 0.25 | total: 500000


I find it interesting that not once have I had the bit-wise AND beat either of the other two. I suppose it's a good thing that all of them take right around the same amount of time.

(php.ini: precision=4)
PHP Version 5.2.6-3ubuntu4.5
Linux sr1123wm 2.6.28-19-server #61-Ubuntu SMP Thu May 27 00:25:35 UTC 2010 i686
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

I just ran the code above:
Terminal:
Code: [ Select ]
$i % 2: 0.15300297737122 | total: 500000
$j & 1: 0.13465905189514 | total: 500000
$k << 31: 0.15124988555908 | total: 999999
  1. $i % 2: 0.15300297737122 | total: 500000
  2. $j & 1: 0.13465905189514 | total: 500000
  3. $k << 31: 0.15124988555908 | total: 999999

Apache:
Code: [ Select ]
$i % 2: 0.16692686080933 | total: 500000
$j & 1: 0.14606189727783 | total: 500000
$k << 31: 0.16066813468933 | total: 999999
  1. $i % 2: 0.16692686080933 | total: 500000
  2. $j & 1: 0.14606189727783 | total: 500000
  3. $k << 31: 0.16066813468933 | total: 999999
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

I wonder why you got 999999 for that last one, Spoof. :scratchhead: My best guess is that you're using a 64-bit processor and shifting 31 bits still always leaves a bit to evaluate to TRUE in the resulting 33 bits.
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

Ya

Code: [ Select ]
me@server: uname -m
x86_64
  1. me@server: uname -m
  2. x86_64

Post Information

  • Total Posts in this topic: 7 posts
  • Users browsing this forum: No registered users and 156 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.