如何衡量用 PHP 编写的代码的速度? [英] How can I measure the speed of code written in PHP?

查看:21
本文介绍了如何衡量用 PHP 编写的代码的速度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能说哪一类(都做同样的工作)执行得更快?有什么软件可以测量吗?

解决方案

你有(至少)两个解决方案:

相当天真"的方法是使用 microtime(true) tobefore 和 after 一部分代码,以获取执行期间经过的时间;其他答案已经说了,并且已经给出了例子,所以我就不多说了.

如果您想对几个指令进行基准测试,这是一个不错的解决方案;例如,比较两种类型的函数——最好进行数千次,以确保对任何扰动元素"进行平均.

像这样,所以,如果你想知道序列化一个数组需要多长时间:

$before = microtime(true);对于 ($i=0 ; $i<100000 ; $i++) {序列化($list);}$after = microtime(true);echo ($after-$before)/$i ." 秒/序列化
";

不完美,但很有用,而且设置时间也不长.


<小时>

如果您想确定哪个函数在整个脚本中花费大量时间,另一个解决方案非常有效,是使用:


  • (来源:sourceforge.net)

    您将获得与 PHP 脚本完全相同的东西 ;-)
    (使用 KCacheGrind,我的意思是;WinCacheGrind 不如 KCacheGrind...)

    这使您可以很好地了解应用程序中哪些需要花费时间——有时它绝对有助于定位使一切变慢的功能^^

    注意 Xdebug 计算 PHP 花费的 CPU 时间;当 PHP 正在等待来自数据库的答案时(例如),它不起作用;只有等待.所以 Xdebug 会认为 DB 请求不需要太多时间!
    这应该在 SQL 服务器上进行分析,而不是在 PHP 上进行分析,所以...


    希望这有帮助:-)
    玩得开心!

    How can I say which class of many (which all do the same job) execute faster? is there a software to measure that?

    解决方案

    You have (at least) two solutions :

    The quite "naïve" one is using microtime(true) tobefore and after a portion of code, to get how much time has passed during its execution ; other answers said that and gave examples already, so I won"t say much more.

    This is a nice solution if you want to benchmark a couple of instructions ; like compare two types of functions, for instance -- it's better if done thousands of times, to make sure any "perturbating element" is averaged.

    Something like this, so, if you want to know how long it take to serialize an array :

    $before = microtime(true);
    
    for ($i=0 ; $i<100000 ; $i++) {
        serialize($list);
    }
    
    $after = microtime(true);
    echo ($after-$before)/$i . " sec/serialize
    ";
    

    Not perfect, but useful, and it doesn't take much time to set up.



    The other solution, that works quite nice if you want to identify which function takes lots of time in an entire script, is to use :

    • The Xdebug extension, to generate profiling data for the script
    • Software that read the profiling data, and presents you something readable. I know three of those :
      • Webgrind ; web interface ; should work on any Apache+PHP server
      • WinCacheGrind ; only on windows
      • KCacheGrind ; probably only Linux and linux-like ; That's the one I prefer, btw

    To get profiling files, you have to install and configure Xdebug ; take a look at the Profiling PHP Scripts page of the documentation.

    What I generally do is not enable the profiler by default (it generates quite big files, and slows things down), but use the possibility to send a parameter called XDEBUG_PROFILE as GET data, to activate profiling just for the page I need.
    The profiling-related part of my php.ini looks like this :

    xdebug.profiler_enable = 0              ; Profiling not activated by default
    xdebug.profiler_enable_trigger = 1      ; Profiling activated when requested by the GET parameter
    xdebug.profiler_output_dir = /tmp/ouput_directory
    xdebug.profiler_output_name = files_names
    

    (Read the documentation for more informations)

    This screenshot is from a C++ program in KcacheGrind :
    (source: sourceforge.net)

    You'll get exactly the same kind of thing with PHP scripts ;-)
    (With KCacheGrind, I mean ; WinCacheGrind is not as good as KCacheGrind...)

    This allows you to get a nice view of what takes time in your application -- and it sometimes definitly helps to locate the function that is slowing everything down ^^

    Note that Xdebug counts the CPU time spent by PHP ; when PHP is waiting for an answer from a Database (for instance), it is not working ; only waiting. So Xdebug will think the DB request doesn't take much time !
    This should be profiled on the SQL server, not PHP, so...


    Hope this is helpful :-)
    Have fun !

    这篇关于如何衡量用 PHP 编写的代码的速度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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