猿问

测量php脚本执行时间的准确方法

我想知道一个PHP for循环执行需要多少毫秒。


我知道通用算法的结构,但是不知道如何在PHP中实现它:


Begin

init1 = timer(); // where timer() is the amount of milliseconds from midnight

the loop begin

some code

the loop end

total = timer() - init1;

End


ABOUTYOU
浏览 692回答 3
3回答

繁星淼淼

您可以使用此microtime功能。从文档中:microtime —返回当前的Unix时间戳(以微秒为单位)如果get_as_float将设置为TRUE,则microtime()返回一个浮点数,它表示自Unix纪元以来精确到最接近的微秒的当前时间(以秒为单位)。用法示例:$start = microtime(true);while (...) {}$time_elapsed_secs = microtime(true) - $start;

繁花如伊

您可以microtime(true)通过以下方式使用:将其放在您的php文件的开头://place this before any script you want to calculate time$time_start = microtime(true);//您的脚本代码在这里// do something把它放在你的php文件的末尾:// Display Script End time$time_end = microtime(true);//dividing with 60 will give the execution time in minutes other wise seconds$execution_time = ($time_end - $time_start)/60;//execution time of the scriptecho '<b>Total Execution Time:</b> '.$execution_time.' Mins';它将输出结果minutes。
随时随地看视频慕课网APP
我要回答