简体   繁体   English

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

[英]Calculate the time needed to run a certain function

I that possible? 我有可能吗? for eg. 例如。 if I want to test if str_replace() is faster that preg_replace() ? 如果我想测试str_replace()是否比preg_replace()更快?

The easy way: 简单的方法:

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

// Your code here

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

The hard(er) way: Use a code profiler to see exactly how much time your methods will take. 硬(呃)方式:使用代码分析器确切地了解您的方法需要多长时间。

You can run the same line 10,000 times (or more) in your script, and use microtime(true) to tell the time it took: 您可以在脚本中运行相同的行10,000次(或更多),并使用microtime(true)来告诉它花费的时间:

microtime() microtime中()

I found this answer by 'bisko' in this thread. 我在这个帖子中找到了'bisko'的答案。

$start = microtime(true); $ start = microtime(true);

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

$end = microtime(true); $ end = microtime(true);

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

The for-loop can be replaced by whatever you want to time. for循环可以替换为您想要的任何时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM