我对@pikzen(2012 年)获得的结果有点惊讶,所以我使用strtotime()
. 我认为这个问题不是从正确的角度考虑的。
如果我们要使用strtotime()
时间戳,则应在执行任何操作之前从头开始转换时间戳。
EG我目前正在编写一个算法,该算法会在很长一段时间内创建重复的停电和假期,当然,需要大量的日期验证和操作。
我使用 PHP 7 进行了 1,000,000 次重复的小测试,结果如下:
对于必须处理大量日期操作的复杂算法,在我看来,处理整数时间戳而不是字符串 DateTimes 有一点好处。
这是用于我的测试的代码:
set_time_limit(180); //3 minutes
echo "<style>table, tr, th, td {border-style:solid; padding:2px;}</style>" .
"<h1>Date add using strtotime() 1,000,000 repetitions</h1>" .
"<table><b><tr style='background-color:LavenderBlush;'><th>Unit</th>" .
"<th>String date with conversion</th><th>String date w/o conversion</th>" .
"<th>Timestamp with conversion</th><th>timestamp w/o conversion</th></tr></b>";
//add day
echo "<tr style='background-color:aqua;'><td><b>Day</b></td>";
$date = date("Y-m-d");
$test = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
$date = date('Y-m-d', strtotime($date . '+1 day'));
}
echo "<td>" . (microtime(true) - $test) . "</td>";
$date = date("Y-m-d");
$test = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
$newDate = strtotime($date . '+1 day');
}
echo "<td>" . (microtime(true) - $test) . "</td>";
$date = strtotime(date("Y-m-d"));
$test = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
$newDate = date('Y-m-d', strtotime('+1 day', $date));
}
echo "<td>" . (microtime(true) - $test) . "</td>";
$date = strtotime(date("Y-m-d"));
$test = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
$date = strtotime('+1 day', $date);
}
echo "<td>" . (microtime(true) - $test) . "</td><tr>";
//add week
echo "<tr style='background-color:LightCyan;'><td><b>Week</b></td>";
$date = date("Y-m-d");
$test = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
$date = date('Y-m-d', strtotime($date . '+1 week'));
}
echo "<td>" . (microtime(true) - $test) . "</td>";
$date = date("Y-m-d");
$test = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
$newDate = strtotime($date . '+1 week');
}
echo "<td>" . (microtime(true) - $test) . "</td>";
$date = strtotime(date("Y-m-d"));
$test = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
$newDate = date('Y-m-d', strtotime('+1 week', $date));
}
echo "<td>" . (microtime(true) - $test) . "</td>";
$date = strtotime(date("Y-m-d"));
$test = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
$date = strtotime('+1 week', $date);
}
echo "<td>" . (microtime(true) - $test) . "</td><tr>";
//add month
echo "<tr style='background-color:LightGreen;'><td><b>Month</b></td>";
$date = date("Y-m-d");
$test = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
$date = date('Y-m-d', strtotime($date . '+1 month'));
}
echo "<td>" . (microtime(true) - $test) . "</td>";
$date = date("Y-m-d");
$test = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
$newDate = strtotime($date . '+1 month');
}
echo "<td>" . (microtime(true) - $test) . "</td>";
$date = strtotime(date("Y-m-d"));
$test = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
$newDate = date('Y-m-d', strtotime('+1 month', $date));
}
echo "<td>" . (microtime(true) - $test) . "</td>";
$date = strtotime(date("Y-m-d"));
$test = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
$date = strtotime('+1 month', $date);
}
echo "<td>" . (microtime(true) - $test) . "</td><tr>";
//add year
echo "<tr style='background-color:LightGoldenRodYellow;'><td><b>Year</b></td>";
$date = date("Y-m-d");
$test = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
$date = date('Y-m-d', strtotime($date . '+1 year'));
}
echo "<td>" . (microtime(true) - $test) . "</td>";
$date = date("Y-m-d");
$test = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
$newDate = strtotime($date . '+1 year');
}
echo "<td>" . (microtime(true) - $test) . "</td>";
$date = strtotime(date("Y-m-d"));
$test = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
$newDate = date('Y-m-d', strtotime('+1 year', $date));
}
echo "<td>" . (microtime(true) - $test) . "</td>";
$date = strtotime(date("Y-m-d"));
$test = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
$date = strtotime('+1 year', $date);
}
echo "<td>" . (microtime(true) - $test) . "</td><tr></table>";
//string date comparation
echo "<br /><h1>Date comparission with 1,000,000 repetition</h1>";
$date = date("Y-m-d");
$i = 0;
$newDate = date('Y-m-d', strtotime($date . '+1 year'));
$test = microtime(true);
while ($i < 1000000) {
if ($date !== $newDate) {
$i++;
}
}
echo "<br / ><b>String date comparission:</b> " . (microtime(true) - $test) . "<br />";
//timestamp comparision
$date = strtotime($date);
$i = 0;
$newDate = strtotime($newDate);
$test = microtime(true);
while ($i < 1000000) {
if ($date !== $newDate) {
$i++;
}
}
echo "<b>timestamp comparission:</b> " . (microtime(true) - $test) . "<br />";