Xeoncross Posted August 17, 2007 Share Posted August 17, 2007 Hello! Well, this afternoon finds me working with the microtime() function as I am trying to find the best way to time my scripts. I want to figure out the fastest/most reliable way to find the execution time of my scripts. So far I have been playing with the following code: <?php $time_start = array_sum(explode(' ', microtime())); $total_time = round(array_sum(explode(' ', microtime())) - $time_start, 6); echo $total_time; ?> <br /> <?php $time = microtime(true); $total_time = (microtime(true) - $time); echo $total_time; ?> <br /> <?php // Function to calculate script execution time. function microtime_float () { list ($msec, $sec) = explode(' ', microtime()); $microtime = (float)$msec + (float)$sec; return $microtime; } $start = microtime_float(); $end = microtime_float(); // Print results. echo $end - $start; ?> which give me on average (I think) the following times: 7.6E-05 6.9141387939453E-06 5.0067901611328E-05 or 0.000039439557949823496 0.000009153168140048553 0.00031783597286984895 More often I get: 8E-05 6.9141387939453E-06 3.3140182495117E-05 In case you don't know - those are exponents. http://www.webwinder.com/wwhtmbin/jexpont.html As you can see, the last way is very poor in scoring a fast time - so that means that I am leaning toward the second way - but I just wanted to know if anyone could time a script faster. Link to comment https://forums.phpfreaks.com/topic/65504-exicution-time-of-your-scripts/ Share on other sites More sharing options...
Xeoncross Posted August 17, 2007 Author Share Posted August 17, 2007 This code seems to work better. <?php // Function to calculate script execution time. function microtime_float () { list ($msec, $sec) = explode(' ', microtime()); $microtime = (float)$msec + (float)$sec; return $microtime; } ?> <table> <tr> <td> <?php $time_start = array_sum(explode(' ', microtime())); sleep(1); $total_time = round(array_sum(explode(' ', microtime())) - $time_start, 6); echo $total_time; ?> </td> <td> <?php $time_start = array_sum(explode(' ', microtime())); sleep(1); $total_time = round(array_sum(explode(' ', microtime())) - $time_start, 6); echo $total_time; ?> </td> </tr> <tr> <td> <?php $time = microtime(true); sleep(1); $total_time = (microtime(true) - $time); echo $total_time; ?> </td> <td> <?php $time = microtime(true); sleep(1); $total_time = (microtime(true) - $time); echo $total_time; ?> </td> </tr> <tr> <td> <?php $time = microtime(); sleep(1); $total_time = (microtime() - $time); echo $total_time; ?> </td> <td> <?php $time = microtime(); sleep(1); $total_time = (microtime() - $time); echo $total_time; ?> </td> </tr> <tr> <td> <?php // Get starting time. $start = microtime_float(); sleep(1); $end = microtime_float() - $start; echo $end; ?> </td> <td> <?php // Get starting time. $start = microtime_float(); sleep(1); $end = microtime_float() - $start; echo $end; ?> </td> </tr> </table> Link to comment https://forums.phpfreaks.com/topic/65504-exicution-time-of-your-scripts/#findComment-327092 Share on other sites More sharing options...
Xeoncross Posted August 17, 2007 Author Share Posted August 17, 2007 Ok, after more testing I found that microtime(true) seems to be the fastest way to count script time. 0.086866855621338 0.077500104904175 0.078529119491577 0.077292919158936 Here is the code: <?php function keep_counting() { $x=0; while($x < 100000) { $x = $x + $x - $x + 1; $b = $x*1000/3; $b *= 999; } } ?> <table cellpadding="5" border="1"> <tr> <td> <?php $time_start = array_sum(explode(' ', microtime())); keep_counting(); echo array_sum(explode(' ', microtime())) - $time_start; ?> </td> <td> <?php $time = microtime(true); keep_counting(); echo (microtime(true) - $time); ?> </td> </tr> <tr> <td> <?php $time_start = array_sum(explode(' ', microtime())); keep_counting(); echo array_sum(explode(' ', microtime())) - $time_start; ?> </td> <td> <?php $time = microtime(true); keep_counting(); echo (microtime(true) - $time); ?> </td> </tr> </table> Link to comment https://forums.phpfreaks.com/topic/65504-exicution-time-of-your-scripts/#findComment-327114 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.