[英]Calculate the time needed to run a certain function
我有可能嗎? 例如。 如果我想測試str_replace()
是否比preg_replace()
更快?
簡單的方法:
$time = microtime(true); // time in Microseconds
// Your code here
echo (microtime(true) - $time) . ' elapsed';
硬(呃)方式:使用代碼分析器確切地了解您的方法需要多長時間。
您可以在腳本中運行相同的行10,000次(或更多),並使用microtime(true)
來告訴它花費的時間:
我在這個帖子中找到了'bisko'的答案。
$ start = microtime(true);
for(...){....}
$ end = microtime(true);
echo($ end - $ start)。' 秒;
for循環可以替換為您想要的任何時間。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.