What actualy will work faster: (int $a = $b; - somewhere in the script)
$c = rand($a, $;
or
if ($a != $ $c = rand($a, $; else $c = $a;
So, speaking in other words, I do understand that most likely the "if" is faster then "rand", BUT the question is will the "rand" run it's generating mechanism as usual, or maybe it'll just realize that both of it's arguments are equal and won't consume all the resources it usualy does?
No! It's just an example, so that it'll be clear what am I asking. Ofcourse this isn't exactly what am I doing. I was just trying to illustrate that IF I'll get this king of situation then what is more efficient?
----- "I think your really splitting hairs here..." Yes, maybe but I'm pretty conserned about it because further I'll have to repeat a cycle about 150 times refering to 15 000 objects. And if you'll multiply those... This is why I'd like to know - can I win something here or not?
so why don't you do some tests, and tell us all what you find. i don't think many people have tested what works faster. i expect if there is a difference, its only going to be a matter of a few percent anyway...
I'v done my tests on a cycle of 50000000 iterations so that the execution time will be noticeable. timerX for the time mesurment. Actualy nothing revolutionary, but I'v found an interesting fact - for me anyway.
1) rand($a, $: $a==$b is a bit faster then $a != $b, approximately 2%, but I'm not sure what's the reason for that: arguments or that it's just one of the explorer windows was refreshing a page.
2) if ($a == $ $c = $a; else $c = rand($a, $; At this case as I'v shown in the example before we have a 100% of "true" in the if condition so that we avoid the rand and check is it faster to check the arguments or just run a rand even on a same arguments. And the result is that it is faster to run the rand anyway, without checking, but again not criticaly - 4.2%
But at the end I was realy surprised! Because I'v always thought that if-else statement works in a way that if the condition is true some block is being executed and that's it, it forgets about the else... but if we compare:
$a = $b
if ($a == $ $c = $a; else rand($a, $;
and
if ($a == $ $c = $a;
We get that... take a deep breath.... the second one is faster then the first one by 151.09%!!! Am I missing something obvious here??
Yes the mel interpretter needs to parse the else statement anyway. Thats probably the cause? try comparing it to a if clause withrand outside the if. (also rand isnt realy that expensive try with a bigger block of code to se how it builds up).
But yeah because mel is a interpretted language the way in wich you write it affects the thing too.