Which is faster php date functions or carbon?



PHP Snippet 1:

<?php

require "Carbon.php";

use Carbon\Carbon;

$carbonTime = 0;
for ($i = 0; $i < 100000; $i++)
{
    $start = microtime(true);
    $time = Carbon::now();  
    $end = microtime(true);

    $carbonTime += $end - $start;
}

echo "carbonTime: ".$carbonTime."\n";

$phpTime = 0;
for ($i = 0; $i < 100000; $i++)
{
    $start = microtime(true);
    $time = new \DateTime();
    $end = microtime(true);

    $phpTime += $end - $start;
}

echo "phpTime: ".$phpTime."\n";

PHP Snippet 2:

$ php test.php
carbonTime: 5.1191372871399
phpTime: 0.42734241485596

$ php test.php
carbonTime: 5.05357670784
phpTime: 0.41754531860352

$ php test.php
carbonTime: 5.4670262336731
phpTime: 0.42954564094543

$ php test.php
carbonTime: 5.0321266651154
phpTime: 0.44966721534729

$ php test.php
carbonTime: 5.1405448913574
phpTime: 0.4540810585022

PHP Snippet 3:

$ php test.php
carbonTime: 0.72775316238403
phpTime: 0.27025842666626

$ php test.php
carbonTime: 0.75773358345032
phpTime: 0.27719449996948

$ php test.php
carbonTime: 0.75334858894348
phpTime: 0.26076078414917

$ php test.php
carbonTime: 0.75232911109924
phpTime: 0.26331186294556

$ php test.php
carbonTime: 0.75696325302124
phpTime: 0.27505803108215

PHP Snippet 4:

<?php

require "Carbon.php";

use Carbon\Carbon;

$carbonTime = 0;
for ($i = 0; $i < 100000; $i++)
{
    $start = microtime(true);
    $time = Carbon::now();
    $time->addMinute();
    $end = microtime(true);

    $carbonTime += $end - $start;
}
echo "carbonTime: ".$carbonTime."\n";

$phpTime = 0;
for ($i = 0; $i < 100000; $i++)
{
    $start = microtime(true);
    $time = new \DateTime();
    $time->add(new DateInterval('PT1M'));
    $end = microtime(true);

    $phpTime += $end - $start;
}

echo "phpTime: ".$phpTime."\n";

$carbonPhpTime = 0;
for ($i = 0; $i < 100000; $i++)
{
    $start = microtime(true);
    $time = Carbon::now();
    $time->add(new DateInterval('PT1M'));
    $end = microtime(true);

    $carbonPhpTime += $end - $start;
}
echo "carbonPhpTime: ".$carbonPhpTime."\n";

PHP Snippet 5:

$ php test.php
carbonTime: 1.9114277362823
phpTime: 0.33648467063904
carbonPhpTime: 0.85358047485352

$ php test.php
carbonTime: 2.0271127223969
phpTime: 0.35125756263733
carbonPhpTime: 0.90319967269897

$ php test.php
carbonTime: 1.8688952922821
phpTime: 0.33922410011292
carbonPhpTime: 0.85987377166748

$ php test.php
carbonTime: 1.8911855220795
phpTime: 0.33247566223145
carbonPhpTime: 0.86109066009521

$ php test.php
carbonTime: 1.8757562637329
phpTime: 0.33344697952271
carbonPhpTime: 0.84496641159058