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

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

问题描述

我如何说很多(哪一个都执行相同的工作)中的哪一个执行得更快?有软件可以测量吗?

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

推荐答案

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

一个相当幼稚"的方法是在一部分代码之前和之后使用microtime(true),以获取在其执行过程中经过了多少时间;其他答案已经说过了,并且已经给出了例子,所以我就不多说了.

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\n";

并不完美,但很有用,并且不需要花费很多时间进行设置.

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 :

  • Xdebug 扩展名,以生成脚本的分析数据
  • 读取概要分析数据并为您呈现可读性的软件.我知道其中三个:
    • Webgrind ;网络界面;应该可以在任何Apache + PHP服务器上运行
    • WinCacheGrind ;仅在Windows上
    • KCacheGrind ;大概只有Linux和类似linux的;我喜欢的就是那个,顺便说一句
    • 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

      要获取配置文件,您必须安装和配置Xdebug;看看文档的剖析PHP脚本页面.

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

      我通常所做的是默认情况下不启用分析器(它会生成很大的文件,并降低速度),但会使用可能性将名为XDEBUG_PROFILE的参数作为GET数据发送,仅为我需要的页面激活分析.
      我的php.ini中与分析相关的部分看起来像这样:

      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
      

      (有关更多信息,请阅读文档)

      此屏幕快照来自KcacheGrind中的C ++程序:
      (来源: sourceforge.net )

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

      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 ^^

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

      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天全站免登陆