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

Ale*_*lex 9 php performance function

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

The*_*zoo 28

简单的方法:

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

// Your code here

echo (microtime(true) - $time) . ' elapsed';
Run Code Online (Sandbox Code Playgroud)

硬(呃)方式:使用代码分析器确切地了解您的方法需要多长时间.


nop*_*ole 15

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

microtime中()


Tin*_*ank 6

我在这个帖子中找到了'bisko'的答案.

$ start = microtime(true);

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

$ end = microtime(true);

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

for循环可以替换为您想要的任何时间.