Home > php教程 > php手册 > PHP关于传众多参数还是传上下文对象的性能测试

PHP关于传众多参数还是传上下文对象的性能测试

WBOY
Release: 2016-06-13 11:31:34
Original
1403 people have browsed it

  在开发微信公众平台平台的过程中,有这么几个参数总是需要传来传去,$userOpenId,$message,$time。

  在整个程序的运行过程中,为了函数方便的处理,将这三个变量一直放在参数列表里。关于这点,我突然联想到在Android里面,总是有一个Context(上下文)对象传来传去的,现在我的理解是,Context里面存放了一些必要的数据,在整个程序执行的逻辑中都有可能被使用。

  因此,我就思考了一下,我这个三个变量要不要包装成Context呢?性能上会不会有提升?

  PHP的函数参数如果是变量的话,都是直接传变量的拷贝,如果是传对象的话,是传对象的指针。也就是说,如果定义了一个String,放到一个函数里去修改,如果不以返回值的形式返回的话,该变量没有被修改,被修改的是该变量的一个拷贝。如果是定义一个对象放到函数中去操作,修改的直接就是这个对象本身。

  由上面的思考,我认为如果传对象的话,可能会更快一点(因为不需要复制)。于是,我写了如下代码来测试。

<?<span php

</span><span //</span><span 程序开始</span>
<span $time1</span> = <span microtime</span><span ();

</span><span //</span><span 上下文环境的三个变量</span>
<span $userOpenId</span> = "abcdefghijk"<span ;
</span><span $message</span> = "我想要查询什么"<span ;
</span><span $time</span> = <span time</span><span ();

</span><span //</span><span 构建上下文对象</span>
<span $context</span> = <span new</span> Context(<span $userOpenId</span>, <span $message</span>, <span $time</span><span );

</span><span //</span><span 循环次数</span>
<span $timeOfLoop</span> = 5000<span ;

</span><span for</span> (<span $i</span> = 1; <span $i</span> <= <span $timeOfLoop</span>; <span $i</span>++<span ) {
    </span><span //</span><span handleMessageByParams($userOpenId, $message, $time);</span>
    handleMessageByObject(<span $context</span><span );
}

</span><span //</span><span 程序结束</span>
<span $time2</span> = <span microtime</span><span ();

</span><span //</span><span 耗时</span>
<span echo</span> "cast:".(<span $time2</span> - <span $time1</span><span );

</span><span //</span><span 通过传参数的处理方法</span>
<span function</span> handleMessageByParams(<span $userOpenId</span>, <span $message</span>, <span $time</span><span ) {
    </span><span echo</span> "working......[".<span $userOpenId</span>.<span $message</span>.<span $time</span>."]\n"<span ;
    </span><span //</span><span 100    0.00020699999999996
    //1000    0.002683
    //5000    0.011099</span>
<span }

</span><span //</span><span 通过传对象的处理方法</span>
<span function</span> handleMessageByObject(<span $context</span><span ) {
    </span><span echo</span> "working......[".<span $context</span>->userOpenId.<span $context</span>->message.<span $context</span>-><span time</span>."]\n"<span ;
    </span><span //</span><span 100    0.00024099999999999
    //1000    0.002897
    //5000    0.014896</span>
<span }

</span><span //</span><span 上下文对象</span>
<span class</span><span  Context {

    </span><span public</span> <span $userOpenId</span> = <span null</span><span ;
    </span><span public</span> <span $message</span> = <span null</span><span ;
    </span><span public</span> <span $time</span> = <span null</span><span ;

    </span><span public</span> <span function</span> __construct(<span $userOpenId</span>, <span $message</span>, <span $time</span><span ) {
        </span><span $this</span>->userOpenId = <span $userOpenId</span><span ;
        </span><span $this</span>->message = <span $message</span><span ;
        </span><span $this</span>-><span time</span> = <span $time</span><span ;
    }
}
</span>?>
Copy after login

  测试的结果我已经在注释中写出,单位是ms(毫秒)。可见,传Context反而变慢了一点。当然,考虑到这个上下文的环境也不可能在一次代码中传来传去传5000次,而且性能差异也是非常小的毫秒级,所以,使用上下文对象来传递必须的参数是一个更优雅的选择。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template