Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP microtime strange behavior
    text
    copied!<p>I was looking at execution time of some functions, but i have found that microtime is getting wrong, in one <code>microtime()</code> </p> <p><strong>implementation №1</strong> always first time is always getting more then second <code>microtime()</code> executing, when i testing one function and saw that one case is faster then another, but after place replace (<em>2nd function to 1st place</em>), it getting slower anyway, even if it was 3x faster...</p> <pre><code>function dotimes($times,$eval,$data=false) {for(;--$times;) eval($eval);} $start = microtime(true); dotimes(1000,'$i = $i?$times/2:1;'); printf(": executed : %f\n",microtime(true)-$start); $start = microtime(true); dotimes(1000,'$i = $i?$times/2:1;'); printf(": executed : %f\n",microtime(true)-$start); // 1st case is always slower... </code></pre> <p><strong>implementation №2</strong> iv made sometimes before <code>microtime()</code> as static data storage, but in that case the time of execute is always second time is slower then first (<em>oposite to implementation №1</em>)</p> <pre><code>function get_execution_time() { static $microtime_start = null; return $microtime_start === null ? $microtime_start = microtime(true) : microtime(true) - $microtime_start; } function dotimes($times,$eval,$data=false) {for(;--$times;) eval($eval);} get_execution_time(); dotimes(1000,'$i = $i?$times/2:1;'); printf(": executed : %f\n&lt;br&gt;",get_execution_time()); get_execution_time(); dotimes(1000,'$i = $i?$times/2:1;'); printf(": executed : %f\n&lt;br&gt;",get_execution_time()); //now 2nd case is faster.. </code></pre> <ul> <li>Can somebody tell me <strong>what is going up</strong>? Why these microtimes in one case 1st always slower, and throught static storage 2nd execute is slow down, <strong>WHY</strong>?</li> </ul> <p>ps if someone need tiny mt function here is my <strong>FIXED AND WORKING CORRECT</strong>:</p> <pre><code>function get_mt() {static $mt; return $mt ? microtime(true)-$mt.$mt=null : $mt=microtime(true);} </code></pre> <p>attached : </p> <pre><code>function get_mt() {static $mt; return $mt?microtime(true)-$mt:$mt=microtime(true);} function dotimes($times,$eval,$data=false) {for(;--$times;) eval($eval);} $start = microtime(true); get_mt(); dotimes(10000,'$i = $i?$times/2:1;'); printf(":clean executed : %f\n&lt;br&gt;",microtime(true)-$start); printf(":static executed : %f\n&lt;br&gt;",get_mt()); $start = microtime(true); get_mt(); dotimes(10000,'$i = $i?$times/2:1;'); printf(":clean executed : %f\n&lt;br&gt;",microtime(true)-$start); printf(":static executed : %f\n&lt;br&gt;",get_mt()); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload