Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-间接对象创建的执行时间更快_Php_Performance_Performance Testing - Fatal编程技术网

PHP-间接对象创建的执行时间更快

PHP-间接对象创建的执行时间更快,php,performance,performance-testing,Php,Performance,Performance Testing,我有3个文件,代码如下: Filename: TCObj.php class TCObj { protected $objects; public function __construct(){ $this->objects = array(); } public function register_service($service) { if(!isset($this->objects[$service])) {

我有3个文件,代码如下:

Filename: TCObj.php

class TCObj {
    protected $objects;

    public function __construct(){
        $this->objects = array();
    }

    public function register_service($service) {
        if(!isset($this->objects[$service])) {
            $this->objects[$service] = new $service;
        }
        return $this->objects[$service];            
    }       
}

File Name: TObj.php

class TObj {
    public function __construct(){

    }

    public function hello() {
        echo "Hello!!!";
    }
}


test.php contains the following code: 

require_once("TCObj.php");  
require_once("TObj.php");

$start_time = microtime(true);
$o = new TCObj();
$p = $o->register_service("TObj");
$p->hello();
$end_time = microtime(true);
echo "<br />";
echo $end_time - $start_time;
require_once("TObj.php");

$start_time = microtime(true);
$p = new TObj();    
$p->hello();
$end_time = microtime(true);
echo "<br />";
echo $end_time - $start_time;
Filename:TCObj.php
类TCObj{
受保护的物品;
公共函数构造(){
$this->objects=array();
}
公共功能登记服务($service){
如果(!isset($this->objects[$service])){
$this->objects[$service]=新的$service;
}
返回$this->objects[$service];
}       
}
文件名:TObj.php
托布级{
公共函数构造(){
}
公共函数hello(){
回声“你好!!!”;
}
}
test.php包含以下代码:
需要一次(“TCObj.php”);
需要一次(“TObj.php”);
$start_time=微时间(真);
$o=新的TCObj();
$p=$o->注册服务(“TObj”);
$p->hello();
$end_time=微时间(真);
回声“
”; echo$end_time-$start_time;
当我运行上述代码时,这比我直接使用以下代码创建对象快得多:

Filename: TCObj.php

class TCObj {
    protected $objects;

    public function __construct(){
        $this->objects = array();
    }

    public function register_service($service) {
        if(!isset($this->objects[$service])) {
            $this->objects[$service] = new $service;
        }
        return $this->objects[$service];            
    }       
}

File Name: TObj.php

class TObj {
    public function __construct(){

    }

    public function hello() {
        echo "Hello!!!";
    }
}


test.php contains the following code: 

require_once("TCObj.php");  
require_once("TObj.php");

$start_time = microtime(true);
$o = new TCObj();
$p = $o->register_service("TObj");
$p->hello();
$end_time = microtime(true);
echo "<br />";
echo $end_time - $start_time;
require_once("TObj.php");

$start_time = microtime(true);
$p = new TObj();    
$p->hello();
$end_time = microtime(true);
echo "<br />";
echo $end_time - $start_time;
require_once(“TObj.php”);
$start_time=微时间(真);
$p=新托比();
$p->hello();
$end_time=微时间(真);
回声“
”; echo$end_time-$start_time;
这不应该是反之亦然吗?还是我在检查分析计算时间的错误方法?请注意我的英语,因为我不会说英语:(

我认为后者会更快,但在具有512 MB Ram和单核(Linux)的服务器上测试时,以及在具有4GB Ram和4核(windows)的本地系统上测试时,情况并非如此


我做错了什么?

您是否对大量执行或仅一次执行进行了测试?速度差异大吗?@Kaddath是的,我多次运行了它,最初两个脚本都需要7.xxxx左右的时间,在测试之后。php会减少到1.xxxx-4.xxxx左右,但直接执行的测试总是6.xxxx-8.xxxxxxi看不出有多大的不同与PHP7.2.0rc4的
0.00010490417480469
相比,对于单个迭代而言,
0.00014019012451172
;尽管可以预期,实例化两个对象的第一次速度稍慢一些……但是有太多的波动,我认为这与您误读数字有关……您的每个数字都有一个较短的周期呃,第二次的时间比第二次的时间要多first@MarkBaker哦…现在我明白了:D似乎我的数学也不好我的错,我明白了E-6意味着10^6,这意味着我必须在它前面加上那么多的零,所以5.9604644775391E-6意味着它实际上是0.000000596044775391,比0.000154018400209961快得多感谢help:)[竖起大拇指]您是否对大量执行或仅一次执行进行了测试?速度差异大吗?@Kaddath是的,我运行了多次,最初两个脚本都需要7.xxxx左右,测试之后。php减少到1.xxxx-4.xxxx左右,但直接脚本总是在6.xxxx-8.xxxxI左右。对于单个迭代,我看不出有多大的差异,PHP 7.2.0rc4的
0.00014019012451172
0.00010490417480469
相比;虽然这是意料之中的,但实例化2个对象的第一个对象的速度稍微慢一点。。。。但是有这么多的波动,我想这与你误读数据有关。。。。你的每一个数字第二个的时间都比第一个短first@MarkBaker哦。。现在我明白了:D看来我的数学也不好。。我的错,我明白了,E-6意味着10^6,这意味着我必须在它前面加上那么多零,所以5.9604644775391E-6意味着它实际上是0.000000596044775391,比0.0001541840209961快得多谢谢你的帮助:)[竖起大拇指]