Created
          October 14, 2013 12:38 
        
      - 
      
 - 
        
Save phcostabh/6974938 to your computer and use it in GitHub Desktop.  
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | <?php | |
| date_default_timezone_set('America/Sao_Paulo'); | |
| error_reporting(E_ALL); | |
| ini_set('display_errors', '1'); | |
| function microtimestamp() | |
| { | |
| $timeofday = gettimeofday(); | |
| return $timeofday['sec'] + $timeofday['usec'] / 1000000; | |
| } | |
| function add($x) | |
| { | |
| return $x++; | |
| } | |
| //Setup | |
| unset($hthousand); | |
| for ($i=0;$i<99999;$i++) { | |
| $hthousand[$i] = (rand()%2); | |
| } | |
| //Foreach | |
| $start = microtimestamp(); | |
| foreach ($hthousand as $key => $value) { | |
| $hthousand[$key] = $value++; | |
| } | |
| $end = microtimestamp(); | |
| $result = ($end-$start); | |
| echo "Foreach: ".$result . PHP_EOL; | |
| //Array_Map(Value) | |
| $start = microtimestamp(); | |
| array_map('add',$hthousand); | |
| $end = microtimestamp(); | |
| $result = ($end-$start); | |
| echo "Array_Map(Value): ".$result . PHP_EOL; | |
| //Array_Map(Reference) | |
| $start = microtimestamp(); | |
| array_map(function($x) { return $x++; },$hthousand); | |
| $end = microtimestamp(); | |
| $result = ($end-$start); | |
| echo "Array_Map(Reference): ".$result . PHP_EOL; | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment