fork download
  1. <?php
  2. $a = array();
  3. $time = array('for_loop'=>0, 'array_chunk'=>0, 'while_floor'=>0, 'while_array_slice'=>0);
  4. // for loop
  5. for($c=1;$c<=10;$c++){
  6. $time_start = microtime(true);
  7.  
  8. for($i=1;$i<=100;$i+=2)
  9. $a[]=$i;
  10.  
  11. $two_pair_as_range_compare = function($array, $compare){
  12. for($i=0;$i<count($array);$i+=2){
  13. if( $compare>=$array[$i] && $compare<=$array[$i+1] )
  14. return true;
  15. }
  16. return false;
  17. };
  18.  
  19. for($i=2;$i<=100;$i+=2)
  20. $two_pair_as_range_compare($a, $i);
  21.  
  22. $time_end = microtime(true);
  23.  
  24. $time['for_loop'] += $time_end - $time_start;
  25. }
  26.  
  27. // array_chunk
  28. for($c=1;$c<=10;$c++){
  29. $time_start = microtime(true);
  30.  
  31. // for loop
  32. for($i=1;$i<=100;$i+=2)
  33. $a[]=$i;
  34.  
  35. $two_pair_as_range_compare = function($array, $compare){
  36. $range = array_chunk($array,2);
  37. foreach($range as $eq){
  38. if( $compare>=$eq[0] && $compare<=$eq[1] )
  39. return true;
  40. }
  41. return false;
  42. };
  43.  
  44. for($i=2;$i<=100;$i+=2)
  45. $two_pair_as_range_compare($a, $i);
  46.  
  47. $time_end = microtime(true);
  48.  
  49. $time['array_chunk'] += $time_end - $time_start;
  50. }
  51.  
  52. // while floor
  53. for($c=1;$c<=10;$c++){
  54. $time_start = microtime(true);
  55.  
  56. // for loop
  57. for($i=1;$i<=100;$i+=2)
  58. $a[]=$i;
  59.  
  60. $two_pair_as_range_compare = function($a, $b){
  61. $cc = 0;
  62. while(1){
  63. if( floor($a[$cc]) && floor($a[$cc+1]) ){
  64. if( floor($a[$cc]) < $b && $b < floor($a[$cc+1]) ){
  65. return true; //echo '找到('.$a[$cc].'-'.$a[$cc+1].')';
  66. }
  67. }else{ return false; break;}
  68. $cc=$cc+1;
  69. }
  70. };
  71.  
  72. for($i=2;$i<=100;$i+=2)
  73. $two_pair_as_range_compare($a, $i);
  74.  
  75. $time_end = microtime(true);
  76.  
  77. $time['while_floor'] += $time_end - $time_start;
  78. }
  79.  
  80. // while array_slice
  81. for($c=1;$c<=10;$c++){
  82. $time_start = microtime(true);
  83.  
  84. // for loop
  85. for($i=1;$i<=100;$i+=2)
  86. $a[]=$i;
  87.  
  88. $two_pair_as_range_compare = function($array, $element){
  89. $cnt = 0;
  90. while(($pair = array_slice($array,$cnt,2)) && (count($pair) === 2)){
  91. $cnt += 2;
  92. if($pair[0] <= $element && $pair[1] >= $element){
  93. return true;
  94. }
  95. }
  96. return false;
  97. };
  98.  
  99. for($i=2;$i<=100;$i+=2)
  100. $two_pair_as_range_compare($a, $i);
  101.  
  102. $time_end = microtime(true);
  103.  
  104. $time['while_array_slice'] += $time_end - $time_start;
  105. }
  106.  
  107. print_r($time);exit;
Success #stdin #stdout #stderr 2.81s 24448KB
stdin
Standard input is empty
stdout
Array
(
    [for_loop] => 0.016389608383179
    [array_chunk] => 0.11997652053833
    [while_floor] => 0.034758329391479
    [while_array_slice] => 2.634635925293
)
stderr
PHP Notice:  Undefined offset: 1050 in /home/QeBUPg/prog.php on line 64
PHP Notice:  Undefined offset: 1100 in /home/QeBUPg/prog.php on line 64
PHP Notice:  Undefined offset: 1150 in /home/QeBUPg/prog.php on line 64
PHP Notice:  Undefined offset: 1200 in /home/QeBUPg/prog.php on line 64
PHP Notice:  Undefined offset: 1250 in /home/QeBUPg/prog.php on line 64
PHP Notice:  Undefined offset: 1300 in /home/QeBUPg/prog.php on line 64
PHP Notice:  Undefined offset: 1350 in /home/QeBUPg/prog.php on line 64
PHP Notice:  Undefined offset: 1400 in /home/QeBUPg/prog.php on line 64
PHP Notice:  Undefined offset: 1450 in /home/QeBUPg/prog.php on line 64
PHP Notice:  Undefined offset: 1500 in /home/QeBUPg/prog.php on line 64