PHP empty loop test
Empty For Loop Test Analysis
Objective
Determine the amount of time that is consumed in each part of a for statement given an empty loop. Under the understanding that a statement such as:
for($i=0;$i<$maxCount;$i++) {
}
will do nothing between the braces. It is our goal to determine the amount of time consumed in each element between the parenthesis.
Methodology
The time consumed in a loop for a given value of maxCount can easily be measured making maxCount significantly large and using the system clock to find a time difference between the time stamp prior to the execution and after the execution of the for loop. Since we can't use the system clock inside the for loop without affecting the for loop's performance we will have to use a system of equations to obtain the values.
Given that total time can be approximated by :
Ax+By+Cz=Ttotal
Where:
x : amount of time that it takes to set $i=0
y : amount of time to do the comparison $i<$maxCount
z : amount of time to do the increment $i++
A : the amount of times $i=0 is executed
B : the amount of times the comparison is made
C : the amount of times the increment is performed
We can set a system of equations like :
A1x+B1y+C1z=T1
A2x+B2y+C2z=T2
A3x+B3y+C3z=T3
By performing a set of well crafted tests that change the values for An, Bn and Cn. The system can be solved and the values of x, y and z that satisfy the equations can be obtained.
Considering that $i=0 is executed only once and that $maxCount is very large ($maxCount>>1) we can disregard the impact of Anx. Simplifying the equations to:
B1y+C1z=T1
B2y+C2z=T2
If we set the value of $maxCount=20 million we can run our first test and complete the first equation:
2E7y+2E7z=T1
To obtain our second equation we must modify the loop so there is a difference in the amount of times an operation is performed in comparison to the other. In this case we do the following:
for($i=0;$i<$maxCount;$i++) {
$i++;
$i++;
$i++;
$i++;
$i++;
$i++;
$i++;
$i++;
$i++;
}
In this example there are ten increments for every comparison. We now set the value of $maxCount to 200 million. Since there are ten increments per loop we actually perform only 20 million comparisons. And equation 2 looks like:
2E7y+2E8z=T2
With these two equations it is only a matter of running the tests to obtain the times.
Test
Doing a run that repeats each for loop 5 times and average the times we obtain:
| condition | increment | time | |
| Equation 1 | 20000000 | 20000000 | 2.49 |
| Equation 2 | 20000000 | 200000000 | 11.36 |
Solving the equations we obtain the following values for z and y respectively.
Time per increment : 0.0000000492738678717778
Time per comparison: 0.0000000750540206172222
We can observe that it takes less time to increment that to compare. Based on these results we rewrite the for loop in the following way:
for($i=0;$maxCount-$i;$i++) {
}
Substituting the comparison with a subtraction. In the understanding that a subtraction resulting in 0 will evaluate to false and break out of the loop.
We then run the test again with this change in effect. The time to run a 20 million iteration loop is reduced from 2.49 to 1.92. Thats approximately 77% of the original time. Resulting in nearly a 25% performance boost.
