@pikzen (2012 年) によって得られた結果に少し驚いたので、 を使用して独自のテストを実行しstrtotime()
ました。問題は正しい観点から考えられていなかったと思います。
タイムスタンプを使用する場合はstrtotime()
、操作を行う前にタイムスタンプを最初から変換する必要があります。
EG私は現在、長期間にわたって停電や休日を繰り返すアルゴリズムを書いています。もちろん、多くの日付の検証と操作が必要です。
PHP 7 を使用して 1,000,000 回の繰り返しで小さなテストを実行しました。結果は次のとおりです。

多くの日付操作を処理する必要がある複雑なアルゴリズムの場合、文字列の DateTime ではなく整数のタイムスタンプを処理する方がわずかにメリットがあるように思えます。
テストに使用したコードは次のとおりです。
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 />";