PHP数组效率转换为对象? [英] Efficiency of PHP arrays cast as objects?

查看:201
本文介绍了PHP数组效率转换为对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,PHP stdClass的对象通常比数组要更快一些,当code是深层嵌套足以为它实际上很重要。如何如果我类型转换定义 stdClass的对象上飞的效率受到影响:

  $ VAR =(对象)阵列(一=大于1,两节= GT; 2);

如果在code这样做是要执行很多很多次,我会好起来的明确定义 $ VAR 作为一个对象,而不是:

  $ VAR =新stdClass的();
$ VAR- - 酮= 1;
$ VAR->二= 2;

时的差别可以忽略不计,因为我会再要访问 $ VAR 作为一个对象从那里,无论哪种方式?

编辑:

一个stdClass的是我需要这里的数据类型。我不关心我是否应该使用数组或我是否应该使用stdClass的对象;我更关心的是是否使用(对象)阵列(....)的实例速记 stdClass的是有效的。是的,这是在code将可能执行数千次。


解决方案

 函数基准($ FUNC_NAME,$迭代){
    $开始= microtime中(真);
    为($ I = 0; $ I< $迭代; $ I ++){
        $ FUNC_NAME();
    }
    $结束= microtime中(真);    $ execution_time = $结束 - $开头;    回声$ FUNC_NAME,':',$ execution_time;
}功能standClass(){
    $ OBJ =新stdClass的();
    $ obj-> param_one = 1;
    $ obj-> param_two = 2;
}功能castFromArray(){
    $ OBJ =(对象)阵列('param_one'=大于1,'param_two'=→2);
}
基准('standClass',1000);
基准('castFromArray',1000);基准('standClass,100000);
基准('castFromArray,100000);

输出:

  standClass:0.0045979022979736
castFromArray:0.0053138732910156standClass:0.27266097068787
castFromArray:0.20209217071533

从阵列stdClass的动态铸造在30%左右的效率,但不同的是可以忽略不计仍直到你知道你将要执行该操作10万次(即使如此,你只是在看的十分之一第二,至少在我的机器上)。

因此​​,简而言之,它其实并不重要,绝大多数的时间,但如果这样做,在一个单一的命令定义数组,然后将其类型强制转换为对象。我肯定,除非你已经确定有问题的code的瓶颈(即使在当时,着眼于减少您如果可能的话迭代次数)不会花时间去担心它。

From what I understand, PHP stdClass objects are generally faster than arrays, when the code is deeply-nested enough for it to actually matter. How is that efficiency affected if I'm typecasting to define stdClass objects on the fly:

$var = (object)array('one' => 1, 'two' => 2);

If the code doing this is going to be executed many many times, will I be better off explicitly defining $var as an objects instead:

$var = new stdClass();
$var->one = 1;
$var->two = 2;

Is the difference negligible since I'll then be accessing $var as an object from there on, either way?

Edit:

A stdClass is the datatype I need here. I'm not concerned with whether I should use arrays or whether I should use stdClass objects; I'm more concerned with whether using the (object)array(....) shorthand of instantiating a stdClass is efficient. And yes, this is in code that will be executed potentially thousands of times.

解决方案

function benchmark($func_name, $iterations) {
    $begin = microtime(true);
    for ($i = 0; $i < $iterations; $i++) {
        $func_name();
    }
    $end = microtime(true);

    $execution_time = $end - $begin;

    echo $func_name , ': ' , $execution_time;
}

function standClass() {
    $obj = new stdClass();
    $obj->param_one = 1;
    $obj->param_two = 2;
}

function castFromArray() {
    $obj = (object)array('param_one' => 1, 'param_two' => 2);
}


benchmark('standClass', 1000);
benchmark('castFromArray', 1000);

benchmark('standClass', 100000);
benchmark('castFromArray', 100000);

Outputs:

standClass: 0.0045979022979736
castFromArray: 0.0053138732910156

standClass: 0.27266097068787
castFromArray: 0.20209217071533

Casting from an array to stdClass on the fly is around 30% more efficient, but the difference is still negligible until you know you will be performing the operation 100,000 times (and even then, you're only looking at a tenth of a second, at least on my machine).

So, in short, it doesn't really matter the vast majority of the time, but if it does, define the array in a single command and then type-cast it to an object. I definitely wouldn't spend time worrying about it unless you've identified the code in question as a bottleneck (and even then, focus on reducing your number of iterations if possible).

这篇关于PHP数组效率转换为对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆