Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is faster php date functions or carbon?

Carbon is simple PHP API extension for DateTime. I want to know that we can use datetime functions using by installing carbon via composer.

which is faster php datetime functions or carbon ?

like image 428
Sharad B Avatar asked Mar 30 '17 09:03

Sharad B


People also ask

What is Nesbot Carbon?

An API extension for DateTime that supports 281 different languages.

How do you convert DateTime to date in Carbon?

The $dateTime variable has the current date format, so create a new variable bind it with Carbon class and access the createFromFormat() method here; you can see two parameters. The first param is the date format, and the second is also the date-time format; you have to pass your choice of format in format() .

What does Carbon :: now return?

Carbon::now returns the current date and time and Carbon:today returns the current date. This is a sample output.

Why Carbon is used in laravel?

The Carbon package can be used for many purposes, such as reading the current date and time, changing the default date and time format, finding the difference between two dates, converting the date and time from one timezone to another timezone, etc.


2 Answers

I did some testing regarding your comment comparing DateTime to Carbon functions:

Calling Carbon::now() vs. new \DateTime() 100.000 times:

<?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";

Results from 5 runs (meaning 5x 100.000 calls):

$ 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

Confirming what I initially wrote:

Since Carbon inherits \DateTime it actually adds a little overhead to those calls (Carbon -> DateTime instead of directly DateTime). The main purpose of Carbon is not to be faster than DateTime, but to enhance it's functionality with commonly used functions.

like image 117
ccKep Avatar answered Nov 15 '22 22:11

ccKep


For anyone coming across this thread in 2022 or later, the speed of PHP has improved a lot, in PHP 8.1 the result of the code from ccKep would be:

$ 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

There is a little overhead. However, the overhead is starting to grow when using manipulation methods.

the code where I added some manipulation:

<?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";

the result:

$ 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

As you can see, the difference grows when manipulating. So this can be a pain in the buns if you have to do a lot of date/time manipulation.

You also can see I've added a combination where I instantiate a DateTime object with Carbon (as Carbon is extending The DateTime class) while manipulating with the DateTime method, to see if this has any difference, because 'setTestNow()' is very handy when it comes to unit-testing.

My advice is to be very careful when picking Carbon. You can use it to the fullest if you do not use a lot of it at the same time (for instance on a simple website), but when manipulating a lot while speed is of the essence, my advice would be: Use DateTime, or only Carbon::now() to allow easy testing and leave it's easy-for-use methods for what they are and continue to use DateInterval.

like image 41
SomeOne_1 Avatar answered Nov 15 '22 23:11

SomeOne_1