fork download
  1. <?php
  2.  
  3. $m = range(1,1000000); // from one to a million
  4. $s = range(1,1000000);
  5.  
  6. // Use array_intersect to return all $m values that are also in $s
  7. $tstart = microtime(true);
  8. echo "+--------------------------------------------------+\n";
  9. //print_r (array_intersect($m,$s));
  10. $tend = microtime(true);
  11. (double)$time = $tend - $tstart;
  12.  
  13. echo "| array_intersect took $time \n";
  14.  
  15. // Use array_flip and isset to return all $m values that are also in $s
  16. $tstart = microtime(true);
  17. $f = array_flip($s);
  18. // $u will hold the intersected values
  19. $u = [];
  20. foreach ($m as $v) {
  21. if (isset($f[$v])) $u[] = $v;
  22. }
  23. //print_r ($u);
  24. $tend = microtime(true);
  25. (double)$time = $tend - $tstart;
  26. echo "|\n";
  27. echo "| array_flip took $time \n";
  28. echo "+--------------------------------------------------+";
Success #stdin #stdout 0.09s 82880KB
stdin
Standard input is empty
stdout
+--------------------------------------------------+
|   array_intersect took 3.6954879760742E-5 
|
|   array_flip took 0.039736986160278 
+--------------------------------------------------+