Skip to content

Instantly share code, notes, and snippets.

@phcostabh
Created October 14, 2013 12:38
Show Gist options
  • Save phcostabh/6974938 to your computer and use it in GitHub Desktop.
Save phcostabh/6974938 to your computer and use it in GitHub Desktop.
<?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