Skip to content

Instantly share code, notes, and snippets.

@gaborwho
Last active August 29, 2015 14:17
Show Gist options
  • Save gaborwho/adcfb59ccc1c9dd97848 to your computer and use it in GitHub Desktop.
Save gaborwho/adcfb59ccc1c9dd97848 to your computer and use it in GitHub Desktop.
<?php
$cycles = pow(10, 5);
echo "Execution times for $cycles iterations:" . PHP_EOL;
$code = '$newDate = clone $date;';
measure($cycles, $code);
$code = '$newDate = new DateTime($timestamp, $timezone);';
measure($cycles, $code);
function measure($cycles, $code)
{
echo $code . PHP_EOL;
$startClone = microtime(true);
$date = new DateTime('now', new DateTimeZone('Europe/Budapest'));
$timestamp = '@' . $date->format('U');
$timezone = $date->getTimezone();
for ($i = 0; $i < $cycles; $i++)
{
eval($code);
}
echo microtime(true) - $startClone . PHP_EOL;
}
@gaborwho
Copy link
Author

$ php test.php 
Execution times for 100000 iterations:
$newDate = clone $date;
0.4895339012146
$newDate = new DateTime($timestamp, $timezone);
1.9592518806458

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment