问题描述
Carbon是DateTime的简单PHP API扩展.我想知道我们可以通过composer安装carbon来使用datetime函数.
Carbon is simple PHP API extension for DateTime. I want to know that we can use datetime functions using by installing carbon via composer.
哪个是更快的php datetime函数或carbon?
which is faster php datetime functions or carbon ?
推荐答案
我对您比较DateTime和Carbon函数的注释进行了一些测试:
I did some testing regarding your comment comparing DateTime to Carbon functions:
呼叫Carbon::now()
vs. new \DateTime()
100.000次:
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";
5次运行的结果(意味着5x 100.000次通话):
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
确认我最初写的内容:
由于Carbon继承了\ DateTime,因此实际上为这些调用增加了一些开销(Carbon-> DateTime而不是直接使用DateTime). Carbon的主要目的不是要比DateTime快,而是要通过常用功能来增强其功能.
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.
这篇关于哪个是更快的php date函数或carbon?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!