40 Tips for optimizing your php Code
reinholdweber.com
40 Tips for optimizing your php Code
1–10 of 12 posts
Re: 40 Tips for optimizing your php Code
#2Wow!
function Prime1() { ...calc one million prime numbers...}
static function Prime2() { ...calc one million prime numbers...}
Prime2() will calculate the prime numbers 4 times faster then Prime1()? Cool.Re: 40 Tips for optimizing your php Code
#3What does this mean?
Re: 40 Tips for optimizing your php Code
#4> Set the maxvalue for your for-loops before and not in the loop. What does this mean?
$maxvalue = 100/10; for($i=0; $iis much faster than this:
for($i=0; $ibecause the value is calculated once instead of ten times.
Re: 40 Tips for optimizing your php Code
#5> Set the maxvalue for your for-loops before and not in the loop. What does this mean?
it means that this: $maxvalue = 100/10; for($i=0; $i is much faster than this: for($i=0; $i because the value is calculated once instead of ten times.
> Do not use functions inside of for loop, such as for ($x=0; $x < count($array); $x) The count() function gets called each time.
Re: 40 Tips for optimizing your php Code
#6> Set the maxvalue for your for-loops before and not in the loop. What does this mean?
it means that this: $maxvalue = 100/10; for($i=0; $i is much faster than this: for($i=0; $i because the value is calculated once instead of ten times.
mbp:~/test doug$ time php -r 'for ($i=1; $i
real 0m5.930s
user 0m5.867s
sys 0m0.026s
mbp:~/test doug$ time php -r '$maxval=100000000/10; for ($i=1; $i
The results are a little different with Python (maxval1.py computes inline; maxval2 divides in advance and uses variable inline) mbp:~/test doug$ time python maxval1.py
real 0m2.134s
user 0m1.761s
sys 0m0.276s
mbp:~/test doug$ time python maxval2.py
real 0m1.890s
user 0m1.588s
sys 0m0.278s
Java shows even more of a difference (again, maxval2 sets variable in advance): mbp:~/test doug$ time java maxval1
real 0m0.174s
user 0m0.116s
sys 0m0.040s
mbp:~/test doug$ time java maxval2
real 0m0.531s
user 0m0.125s
sys 0m0.041s
Re: 40 Tips for optimizing your php Code
#7I'd say forget all of this and focus on being a productive developer, building something people want, and ruthlessly avoiding premature optimization. When your application is slow, measure it to find out why, then fix it, and repeat.
1. Let's take #2, "echo is faster than print" - Seriously, is this what we should be worrying about? I can't detect a difference in PHP 5.2.4 across 100,000 echos/prints, so it must not be very big.
2. ++i vs i++ to squeeze out 1 fewer opcode?
3. Using isset($foo{5}) vs a strlen check? I pity the next programmer who has to read and understand the purpose of this code.
Re: 40 Tips for optimizing your php Code
#8Earlier quoted context omitted.
it means that this: $maxvalue = 100/10; for($i=0; $i is much faster than this: for($i=0; $i because the value is calculated once instead of ten times.
That's not necessarily true. If the compiler is smart enough it won't recalculate the value of a numeric expression which doesn't contain any variables. On the other hand, dereferencing a variable isn't without cost. Of course, in most cases you'd be dereferencing variables anyway in the test clause, so the original idea, compute loop limits before entering the loop, is correct.: mbp:~/test doug$ time php -r 'for ($i…
Re: 40 Tips for optimizing your php Code
#9Earlier quoted context omitted.
That's not necessarily true. If the compiler is smart enough it won't recalculate the value of a numeric expression which doesn't contain any variables. On the other hand, dereferencing a variable isn't without cost. Of course, in most cases you'd be dereferencing variables anyway in the test clause, so the original idea, compute loop limits before entering the loop, is correct.: mbp:~/test doug$ time php -r 'for ($i…
Does anyone have any ideas for what's happening in the Python example? Could you post the Python code?
for i in range(1, 100000000/10):
pass
And here is maxval2.py: maxval=100000000/10
for i in range(1, maxval):
pass
Re: 40 Tips for optimizing your php Code
#10Earlier quoted context omitted.
it means that this: $maxvalue = 100/10; for($i=0; $i is much faster than this: for($i=0; $i because the value is calculated once instead of ten times.
That's not necessarily true. If the compiler is smart enough it won't recalculate the value of a numeric expression which doesn't contain any variables. On the other hand, dereferencing a variable isn't without cost. Of course, in most cases you'd be dereferencing variables anyway in the test clause, so the original idea, compute loop limits before entering the loop, is correct.: mbp:~/test doug$ time php -r 'for ($i…
public class maxval1 {
static int $i, $x;
public static void main(String args[]) throws Exception {
for ($x=$i=1; $i
And maxval2.java: public class maxval2 {
static int $a, $i, $x;
public static void main(String args[]) throws Exception {
$a=100000000/10;
for ($x=$i=1; $i