Advertisement
ivansky

PHP Check empty string perfomance test

Apr 5th, 2015
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.76 KB | None | 0 0
  1. <?php
  2.  
  3. $sa = array('some string', '', 'test1', 'test2', '');
  4. $length = count($sa);
  5. $b = false;
  6. $limit = 1000000;
  7. $times = $limit*$length;
  8.  
  9. $start = microtime(true);
  10.  
  11. for($i = 0; $i < $limit; $i++){
  12.     foreach($sa as $s){
  13.         $b = (strlen($s) > 0);
  14.     }
  15. }
  16.  
  17. echo 'strlen() > 0 used '.$times.' times in ' . (microtime(true) - $start) . ' msec' . "\n\n";
  18.  
  19. $start = microtime(true);
  20.  
  21. for($i = 0; $i < $limit; $i++){
  22.     foreach($sa as $s){
  23.         $b = ($s !== '');
  24.     }
  25. }
  26.  
  27. echo 'var !== \'\' used '.$times.' times in ' . (microtime(true) - $start) . ' msec' . "\n\n";
  28.  
  29. $start = microtime(true);
  30.  
  31. for($i = 0; $i < $limit; $i++){
  32.     foreach($sa as $s){
  33.         $b = !empty($s);
  34.     }
  35. }
  36.  
  37. echo '!empty() used '.$times.' times in ' . (microtime(true) - $start) . ' msec' . "\n\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement