计算运行某个功能所需的时间

时间:2010-11-01 11:07:33

标签: php function performance

我有可能吗? 例如。如果我想测试str_replace()是否比preg_replace()更快?

3 个答案:

答案 0 :(得分:26)

简单方法:

$time = microtime(true); // time in Microseconds

// Your code here

echo (microtime(true) - $time) . ' elapsed';

艰难的方式: 使用代码分析器确切了解您的方法将花费多少时间。

答案 1 :(得分:14)

您可以在脚本中运行相同的行10,000次(或更多),并使用microtime(true)来说明所花费的时间:

microtime()

答案 2 :(得分:6)

我在this主题中通过'bisko'找到了这个答案。

  

$ start = microtime(true);

     

for(...){     ....   }

     

$ end = microtime(true);

     

echo($ end - $ start)。'秒;

for循环可以替换为你想要的时间。