ppazos

PHP micro optimizations for is_null vs isset vs === NULL

Apr 22nd, 2012
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.17 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  
  5. By Pablo Pazos @ppazos
  6.  
  7. Test result:
  8. isset 0.69305992126465
  9. is_null 2.0065321922302
  10. == NULL 0.79327797889709
  11. === NULL 0.76104307174683
  12. != NULL 0.79441618919373
  13. !== NULL 0.69468784332275
  14. */
  15.  
  16. $counter = 5000000;
  17. $name    = 'henry';
  18. $start   = microtime(true);
  19. for($i=0; $i<$counter; $i++) {
  20.     isset($name);
  21. }
  22. $end   = microtime(true);
  23. echo "isset ", $end - $start, "<br/>";
  24.  
  25.  
  26. $start   = microtime(true);
  27. for($i=0; $i<$counter; $i++) {
  28.     is_null($name);
  29. }
  30. $end   = microtime(true);
  31. echo "is_null ", $end - $start, "<br/>";
  32.  
  33.  
  34. $start   = microtime(true);
  35. for($i=0; $i<$counter; $i++) {
  36.     $name == NULL;
  37. }
  38. $end   = microtime(true);
  39. echo "== NULL ", $end - $start, "<br/>";
  40.  
  41.  
  42. $start   = microtime(true);
  43. for($i=0; $i<$counter; $i++) {
  44.     $name === NULL;
  45. }
  46. $end   = microtime(true);
  47. echo "=== NULL ", $end - $start, "<br/>";
  48.  
  49.  
  50. $start   = microtime(true);
  51. for($i=0; $i<$counter; $i++) {
  52.     $name != NULL;
  53. }
  54. $end   = microtime(true);
  55. echo "!= NULL ", $end - $start, "<br/>";
  56.  
  57.  
  58. $start   = microtime(true);
  59. for($i=0; $i<$counter; $i++) {
  60.     $name !== NULL;
  61. }
  62. $end   = microtime(true);
  63. echo "!== NULL ", $end - $start, "<br/>";
  64.  
  65. ?>
Advertisement
Add Comment
Please, Sign In to add comment