SHARE
TWEET

Are Yoda-Conditionals faster in PHP?

a guest Oct 17th, 2012 43 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [603] Wed 17.Oct.2012 12:51:42
  2. [kadmin@freebsd-devel][~] cat yoda
  3. <?php
  4.  
  5. function a() {
  6.  
  7.    $x=1;
  8.  
  9.    while ($x<100000000) {
  10.       if (FALSE === $x) {
  11.          $out = "X is false \n";
  12.       } else {
  13.          $out = "X is true \n";
  14.       }
  15.       $x++;
  16.    }
  17. }
  18.  
  19. function b() {
  20.  
  21.    $x=1;
  22.  
  23.    while ($x<100000000) {
  24.       if ($x === FALSE) {
  25.          $out = "X is false \n";
  26.       } else {
  27.          $out = "X is true \n";
  28.       }
  29.       $x++;
  30.    }
  31. }
  32. $start=microtime(1);
  33. a();
  34. $end=microtime(1);
  35. echo "Function a took ".($end-$start). " seconds.\n";
  36.  
  37. $start=microtime(1);
  38. b();
  39. $end=microtime(1);
  40. echo "Function b took ".($end-$start). " seconds.\n";
  41.  
  42. [604] Wed 17.Oct.2012 12:51:44
  43. [kadmin@freebsd-devel][~] php yoda
  44. Function a took 15.374212026596 seconds.
  45. Function b took 16.012656927109 seconds.
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top