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

  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post June 29th, 2010, 2:53 pm

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>
Strong with this one, the sudo is.
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post June 29th, 2010, 2:53 pm

  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post June 29th, 2010, 3:12 pm

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.
Strong with this one, the sudo is.
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Joined: Mar 12, 2007
  • Posts: 6228
  • Loc: South-Africa
  • Status: Offline

Post June 30th, 2010, 5:27 am

on my wamp the script wrote:
$i % 2: 0.27078700065613 | total: 500000
$j & 1: 0.24281907081604 | total: 500000
$k << 31: 0.25018215179443 | total: 500000
Let's leave all our *plum* where it is and go live in the jungle ...
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post June 30th, 2010, 8:24 am

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
Strong with this one, the sudo is.
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Joined: May 22, 2004
  • Posts: 3415
  • Loc: Richland, WA
  • Status: Offline

Post June 30th, 2010, 11:00 am

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
#define NULL (::rand() % 2)
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post June 30th, 2010, 11:50 am

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.
Strong with this one, the sudo is.
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Joined: May 22, 2004
  • Posts: 3415
  • Loc: Richland, WA
  • Status: Offline

Post June 30th, 2010, 12:27 pm

Ya

Code: [ Select ]
me@server: uname -m
x86_64
  1. me@server: uname -m
  2. x86_64
#define NULL (::rand() % 2)

Post Information

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

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.