nette / utils

🛠 Lightweight utilities for string & array manipulation, image handling, safe JSON encoding/decoding, validation, slug or strong password generating etc.
https://doc.nette.org/utils
Other
1.98k stars 147 forks source link

DateTime: build from timestamp use method setTimestamp() #300

Closed h4kuna closed 1 year ago

h4kuna commented 1 year ago

Test how I measure

function measure(callable $test): float
{
    $start = microtime(true);
    for ($i = 0; $i < 1000; ++$i) {
        $test($i);
    }
    return microtime(true) - $start;
}

echo 'Datetime' . PHP_EOL;
echo measure(static function (int $i) {
        return (new DateTime())->setTimestamp($i);
    }) . PHP_EOL;

echo measure(static function (int $i) {
        return (new DateTime('@' . $i))->setTimezone(new \DateTimeZone(date_default_timezone_get()));
    }) . PHP_EOL;

echo 'DatetimeImmutable' . PHP_EOL;
echo measure(static function (int $i) {
        return (new DateTimeImmutable())->setTimestamp($i);
    }) . PHP_EOL;

echo measure(static function (int $i) {
        return (new DateTimeImmutable('@' . $i))->setTimezone(new \DateTimeZone(date_default_timezone_get()));
    }) . PHP_EOL;
dg commented 1 year ago

thanks