1

I was looking at execution time of some functions, but i have found that microtime is getting wrong, in one microtime()

implementation №1 always first time is always getting more then second microtime() executing, when i testing one function and saw that one case is faster then another, but after place replace (2nd function to 1st place), it getting slower anyway, even if it was 3x faster...

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...

implementation №2 iv made sometimes before microtime() as static data storage, but in that case the time of execute is always second time is slower then first (oposite to implementation №1)

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<br>",get_execution_time());
get_execution_time();
dotimes(1000,'$i = $i?$times/2:1;');
printf(": executed : %f\n<br>",get_execution_time());
//now 2nd case is faster..
  • Can somebody tell me what is going up? Why these microtimes in one case 1st always slower, and throught static storage 2nd execute is slow down, WHY?

ps if someone need tiny mt function here is my FIXED AND WORKING CORRECT:

function get_mt() {static $mt; return $mt ? microtime(true)-$mt.$mt=null : $mt=microtime(true);}

attached :

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<br>",microtime(true)-$start);
printf(":static executed : %f\n<br>",get_mt());
$start = microtime(true);
get_mt();
dotimes(10000,'$i = $i?$times/2:1;');
printf(":clean executed : %f\n<br>",microtime(true)-$start);
printf(":static executed : %f\n<br>",get_mt());
Jacob88
  • 95
  • 9
  • just for testing, no matter i can put any function inside, and in 1st case - always 2nd exec faster. In 2nd case always 2nd exec slower. – Jacob88 Jul 28 '12 at 14:48
  • What is the question? Give working examples... – meze Jul 28 '12 at 15:01
  • Put two times `dotimes(1000,'$i = $i?$times/2:1;');` for example, and when via static 2nd iteration would be slower then the same `eval()` code before.. – Jacob88 Jul 28 '12 at 15:09
  • ...What exactly are you trying to test? Cause if you `eval` the code in order to test it, the biggest part of the time spent will most likely be on just parsing the same freaking line of code over and over. You may as well define a function and pass a callback...or even `create_function` in `dotimes`, but that's almost as bad as `eval` as far as security, readability and even timing accuracy go. (You're basically replacing parsing with a function call...which isn't as bad, but still takes enough time to throw off measurements.) – cHao Jul 30 '12 at 19:18

2 Answers2

2

So far, I see that implementation №1 is correct. No clue what you tried in your second implementation.

The advice here - never test two cases in the same script. Run them separately a few times and then find the average time. PHP allocates memory when it needs and this is a slow operation. The second case may reuse already allocated memory and skip this operation and you get wrong results.

meze
  • 14,368
  • 4
  • 45
  • 52
  • Put two different function with the same logic, but one should be faster, and you will see that by static 2nd always slow, and in 1st implementation 2nd always faster %) – Jacob88 Jul 28 '12 at 15:16
  • You will never get that by static 2nd would be faster then 1st, same as with №1 .And how i can get the right way of coding?? – Jacob88 Jul 28 '12 at 15:20
  • @Jacob88 but what did you expect? %) your first implementation: write down the micro time , let's say it's 100, allocate memory for 1 sec (php does it internally), find the difference: 100 - (100+1+1 second takes the code) = 2 seconds. Second case (time has changed to 102): 102-(skip memory + 1) = 1 seconds. Your second implementation: write down and **remember** time: 100 s. Allocate memory and eval code 100-(1+1) = 2 seconds. Time has not changed because you remembered it in static: 100-(skip memory+1) = 1 + 2 seconds that took previous code = 3 seconds. Isn't that what you write in code? ;P – meze Jul 28 '12 at 15:23
  • I have attached 3rd variant with both cases. Look please what it returns. :clean executed : 0.068729:static executed : 0.068740 :clean executed : 0.040131:static executed : 0.108882 – Jacob88 Jul 28 '12 at 15:29
  • At 2nd iteration how is it possible (not memory cause - it started at one time) 0.4 - till - 1 . _wt#_ – Jacob88 Jul 28 '12 at 15:35
  • @Jacob88 can't say anything about 0.4 - till - 1. But your third results are expected too. You remembered starting time in a static variable and it's always bigger than clean. Just to think about it: (0.108882 - 0.040131) = 0.068751. Does that number remind you anything? Yes, it's from here _clean executed : 0.068729:static executed : 0.068740_. That's what i don't understand, you subtract 100 from 110 and then expect the result to be 1. – meze Jul 28 '12 at 15:51
  • Ooops, i have found bug with static function, it should be nulled after executed time returns. If some one need iv edited 1st msg. But now both variants are second executed is always faster then previous... – Jacob88 Jul 28 '12 at 16:05
0

static makes a variable semi global within that function retative to function name.

I would say the delay goes in the fact that the static array needs to be checked for the previous value of the static variable.

extra work == delay

Tschallacka
  • 24,188
  • 10
  • 79
  • 121
  • Ok, i see that static is slower, but WHY when i calling 2nd time this func returns time always bigger then previous??? – Jacob88 Jul 28 '12 at 14:53
  • the nature of static is that it always remembers the previous value of the static. why not try ’static $microtime; $microtime = null’ but that kinda defeats the use of a static. every time you call it it keeps remebering the previous value. – Tschallacka Jul 28 '12 at 15:00
  • Ok forget about static, look at 1st case - the problem is the same, but oposite, why previous is slower? – Jacob88 Jul 28 '12 at 15:04