You are on page 1of 2

PHP: Most Common Mistakes – Using Post Increment Instead of Pre

Increment

The Problem

I want to first point out that the mistake I am about to discuss holds true for many languages. However, I’m going to focus on its presence in PHP because that’s

my favorite language and I would be willing to bet that very few experienced PHP developers, let alone beginners, are aware that they are making this error. It

should also be noted that everything I talk about in this post can also apply to decrementing too.

Incrementing is the process of adding to a numerical variable’s value. Incrementing primarily takes place in loops. In basic practice, incrementing can be achieved

in three different ways in PHP. Here is each method demonstrated using a while loop:
1 // Method 1, using standard variable modification, no shorthand incrementer
2 used:
3 $i = 0;
4 while ($i < 20000)
5 {
6 // do stuff here
7 $i = $i + 1;
8 }
9  
10 // Method 2, using post incrementation, by far the most popular method:
11 $i = 0;
12 while ($i < 20000)
13 {
14 // do stuff here
15 $i++;
16 }
17  
18 // Method 3, using pre incrementation:
19 $i = 0;
20 while ($i < 20000)
21 {
22 // do stuff here
23 ++$i;
}

It first must be pointed out that all three methods here achieve the exact same thing. However, the problem arises when you compare their speeds. Method 1,
although I don’t think it’s widely used, is by far the slowest out of the three. The next slowest method is method 2, which also happens to be by far the most
popular method for incrementing or decrementing a variable. I find this troubling and I see it as a major issue. Method 3 performs the best, hands down, 100% of
the time. If you are curious as to why, I’ll give a brief explanation a little later in this post. 

Why is this a Bad Mistake?

I love efficient code. All developers should love efficient code. I have debated semantics like incrementing methods with experienced developers and I constantly
hear an argument that goes like this: “all of the methods achieve the same thing and in the end, there is only a tiny difference between their speeds. The developer
should use whichever method they feel most comfortable with.” This is a sentiment I strongly reject. The way I see it, when there are three methods of doing
something, and one of them outperforms the other two 100% of the time, it no longer comes down to developer choice about which one they should use. They
should be using the more efficient method. Now, I think it is important to note that in many cases code can be made more efficient by reducing readability or
reducing re-usability and flexibility. I am not suggesting anyone does that. In the case of incrementing, all three methods share similar syntax and most importantly,
they are all one line of code. 

The Solution

Like I have already stated, the solution to this problem is very simple. When merely incrementing or decrementing variable, always, with no exceptions, use pre
incrementation (++$i). Whether it just be on its own line or in a for loop counter, pre incrementation will always be the fastest. However, it needs to be noted that in
other situations that aren’t straight incrmentation, post and pre do serve different purposes. The difference is in the value they return. Pre incrementation returns
the value of the variable after it has been incremented, and post incrementation returns the value before the variable is incremented. In straight
incrementation/decrementation where the returned value is not being used, this being most cases, pre incrementation should always be used because of its
performance advantage. But for any case where the return value is being used, post and pre incrementation need to both be considered to see which will
accomplish the task in the best manner.

I don’t want to get too much into the inner-workings of PHP, but the reason for the performance advantage of pre incrementation is derived from its functionality
when compared to post. Pre incrementation is able to just increment the variable and then return the value. This requires little overhead and no copying of values.
On the other hand, for post incrementation, the value of the variable being modified must first be copied to a temporary location so it can be returned. This of
course requires some overhead and hence decreases performance. 

Conclusion

Pre incrementation and decrementation should always be used when the returned value is irrelevant. Earlier in this post, I described three possible methods of
incrementing a variable. I wanted to speed test them for proof, so I set up a simple test for each method using a while loop set to iterate five-hundred million times.
You are probably asking, “Why five-hundred million iterations?” I decided to make each loop about the size of the Facebook user-base. This makes each loop very
realistic for real-world use. There are many situations where a program will need to iterate through an entire user-base, so it makes sense. 

Here is my simple speed test program: (note that this is not the most effective way to test speed, because it doesn’t use averages, but it gets the job done for this
test, and it’s simple to understand)
1 <?php
2 set_time_limit(500);
3  
4 $test = ' ;
5 $i = 0;
6 $time_start = microtime(true);
7 while ($i < 500000000)
8 {
9 $test = 'a val';
10 $i++;
11 }
12  
13 $time_end = microtime(true);
14 $run_time = $time_end - $time_start;
15 echo 'Run time with i++: ' . $run_time . '<br/>';
16  
17  
18 $test = ' ;
19 $i = 0;
20 $time_start = microtime(true);
21 while ($i < 500000000)
22 {
23 $test = 'a val';
24 ++$i;
25 }
26  
27 $time_end = microtime(true);
28 $run_time = $time_end - $time_start;
29 echo 'Run time with ++i: ' . $run_time . '<br/>';
30  
31 $test = ' ;
32 $i = 0;
33 $time_start = microtime(true);
34 while ($i < 500000000)
35 {
36 $test = 'a val';
37 $i = $i + 1;
38 }
39  
40 $time_end = microtime(true);
41 $run_time = $time_end - $time_start;
42 echo 'Run time with i = i + 1: ' . $run_time .
43 '<br/>';
44  
?>

And the results when I ran it:


Run time with i++: 118.29421687126
Run time with ++i: 112.87914013863
Run time with i = i + 1: 131.7296547889

Just as expected. Pre incrementation sees a fairly significant speed improvement over post incrementation, and normal assignment isn’t even close.

I realize that five-hundred million iterations is a very large loop, but I also contend that efficiency in cases like this is even vital for small examples. Instead of huge
loops, think about concurrent users. If a PHP page is requested by 200 visitors at the same time, the server(s) needs to take care of each request. Why give it
more overhead, no matter how minuscule that overhead might be? Even tiny performance issues are heavily magnified by mass amounts of requests. That is why
I truly believe that a developer should write code as efficiently as possible and anticipate for mass amounts of concurrent requests. You can’t really go wrong with
that philosophy.

You might also like