开发者

Calculate the time needed to run a certain function

开发者 https://www.devze.com 2023-01-22 16:43 出处:网络
I that possible? for eg. if I want to test if str_replace() is faster 开发者_开发知识库that preg_replace() ?The easy way:

I that possible? for eg. if I want to test if str_replace() is faster 开发者_开发知识库that 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.

Reference: microtime()


I found this answer by 'bisko' in this thread.

$start = microtime(true);

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

$end = microtime(true);

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

The for-loop can be replaced by whatever you want to time.

0

精彩评论

暂无评论...
验证码 换一张
取 消