Created
July 9, 2019 16:50
-
-
Save jerodev/42c581e7301ac970313ac97d37647815 to your computer and use it in GitHub Desktop.
PHP datetime manipulation benchmark. https://www.deviaene.eu/benchmarking-datetime-manipulation-in-php
This file contains 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 | |
$count = 25000; | |
$functions = [ | |
'carbon' => function ($i) { | |
return now()->subDays($i)->format('Y-m-d'); | |
}, | |
'strtotime' => function ($i) { | |
return date('Y-m-d', strtotime("$i days ago")); | |
}, | |
'timestamps' => function ($i) { | |
return date('Y-m-d', time() - ($i * 24 * 60 * 60)); | |
} | |
]; | |
$results = []; | |
foreach ($functions as $key => $func) { | |
$time = microtime(true); | |
for ($i = 0; $i < $count; $i++) { | |
$date = $func($i); | |
echo $date . PHP_EOL; | |
} | |
$time = round(microtime(true) - $time, 6); | |
$results[$key] = $time; | |
} | |
foreach ($results as $key => $time) { | |
echo 'Parsed ' . number_format($count, 0, ',', '.') . " dates in $time seconds using $key.\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment