ru en uk

  sing in

(044) 362 48 16   (098) 294 41 60


   Services
   Portfolio
   Prices
   Articles
   Services
   Printing
   Multimedia
   Hosting
   Contacts

1. Switch vs If-Then-Else



Comparison of performance of structures SWITCH and IF-ELSE IF
tested the following code:


var x = 20;
var y = 0;

for (var i = 0; i <100000; i + +) (

if (x == 0) (y = x)
else if (x == 1) (y = x)
else if (x == 2) (y = x)
else if (x == 3) (y = x)
else if (x == 4) (y = x)
else if (x == 5) (y = x)
else if (x == 6) (y = x)
else if (x == 7) (y = x)
else if (x == 8) (y = x)
else if (x == 9) (y = x)
else if (x == 10) (y = x)
else if (x == 11) (y= X)
else if (x == 12) (y = x)
else if (x == 13) (y = x)
else if (x == 14) (y = x)
else if (x == 15) (y = x)
else if (x == 16) (y = x)
else if (x == 17) (y = x)
else if (x == 18) (y = x)
else if (x == 19) (y = x)
else if (x == 20) (y = x)

)

and

var x = 20;
var y = 0;

for (var i = 0; i <100000; i + +) (

switch (x) (

case 0: y = x; break;
case 1: y = x; break;
case 2: y = x; break;
case 3: y = x; break;
case 4: y = x; break;
case 5: y = x; break;
case 6: y = x; break;
case 7: Y = x; break;
case 8: y = x; break;
case 9: y = x; break;
case 10: y = x; break;
case 11: y = x; break;
case 12: y = x; break;
case 13: y = x; break;
case 14: y = x; break;
case 15: y = x; break;
case 16: y = x; break;
case 17: y = x; break;
case 18: y = x; break;
case 20: y = x;
)
)


FireFox: IFTE: 250ms, SWITCH: 25ms Increase productivity: 10.00 times
IE: IFTE: 281ms, SWITCH: 219ms Increase productivity: 1.28 times

the same test with PHP showed a performance in the Spanisholzovanii operator switch to 1.46-fold

2. Switch Structure



The operator switch quickly implemented, if the variables (that is comparing the values) in order and increase predictability.


iter2 = 100000;
var x = 20;

for (var i = 0; i <iter2; i + +) (

switch (x) (

case 0: y = x;
case 1: y = x;
case 2: y = x;
case 3: y = x;
case 4: y = x;
case 5: y = x;
case 6: y = x;
case 7: y = x;
case 8: y = x;
case 9: y = x;
case 10: y = x;
case 12: y = x;
case 13: y = x;
case 14: y = x;
case 15: y = x;
case 16: y = x;
case 17: y = x;
case 18: y = x;
case 19: y = x;
case 20: y = x;
)
)

and

iter2 = 100000;
var x = 200;
var y = 0;

for (var i = 0; i <iter2; i + +) (
switch (x) (

case 0: y = x;
case 9: y = x;
case 23: y = x;
case 35: y = x;
case 41: y = x;
case 50: y = x;
case 62: y = x;
case 70: y = x;
case 87: y = x;
case 91: y = x;
case 102: y = x;
case 111: y = x;
case 125: y = x;
case 130: y = x;
case 149: y = x;
case 152: y = x;
case 161: y = x;
case 171: y = x;
case 183: y = x;
case 190: y = x;
case 199: y = x;
)
)


FireFox: SWITCH without optimization: 78ms, SWITCH optimization: 47ms Increase productivitycies: 1.66 times
IE: SWITCH without optimization: 218ms, SWITCH optimization: 218ms Increase productivity: no

PHP is also in prevents the growth of proivoditelnosti.
in the PHP switch works faster if the variable x does not coincide with any of the values of case ...

3. Look Up Tables


The meaning of the optimization is to minimize the labor intensive operations, such as mathematics. This minimization is that all possible outcomes in advance to put in an array and choose the results of bypass operation calculation.
For example

sin = new Array ();

for (var i = 1; i <= 360; i + +) (
sin = i * (Math.PI/180);
)


in order to select the required angle of the sine example 34 degrees:

var trigVal = sin [34];

Code used in the tests:

create an array of table values

logTable = new Array (100);

for (var i = 0; i <= 99; i + +) (
logTable = Math.log (i);
)

4. Loop Unrolling



The idea is to speed up the completed
s cycles, a vivid example:

for (var i = 0; i <iterations;) (
[do something with i]; i + +;
[do something with i]; i + +;
[do something with i]; i + +;
[do something with i]; i + +;
[do something with i]; i + +;
)


executed much faster than code

for (var i = 0; i <iterations; i + +) (
[do something with i];
)


in PHP is also very well seen an increase in productivity in the use of Loop Unrolling.

5. Reverse Loop Counting


The point is that, comparede variable with the number of running faster than the comparison of a variable with another variable. Also, the author mentions that the comparison of the variable with the number 0 is faster than comparing a variable with any other number.

In this cycle

for (i = 0; i <iterations; i + +) (
/ / Do something here
)


would be better replaced by a cycle

for (var i = iterations; i> 0; i -) (
/ / Do something here
)



Untested code:

CODE 1:

rIter = 500000;
for(var i = rIter; i> 0; i -) (

)


CODE 2:

rIter = 500000;
for (var i = 0; i <rIter; i + +) (

)


FireFox: CODE 2: 219ms, CODE 1: 78ms Increase productivity: 2.80 times
IE: CODE 2: 188ms, CODE 1: 125ms NatureArt performance: 1.50 times

This is true for PHP!!!

6. Loop Flipping



The author states that there are situations where the post-cycle will be faster, but specific examples are given.
This should be avoided, soas post-cycle

CODE1:

var fIter = 500000;
i = 0;
do
(
i + +;
) While (i <fIter);


is slower than the prefix (at least in all javascript tests)

CODE2:

var fIter = 500000;
for (var i = 0; i <fIter; i + +) (

)


FireFox: CODE1: 578ms, CODE2: 219ms Increase productivity: 2.64 times
IE: CODE1: 313ms, CODE2: 187ms Increase productivity: 1.67 times

in PHP inverse situation, post-cycle (do not knowwhy) a little faster.

 
Working with databases
29.05.2007
Safety Fundamentals
29.05.2007
Introduction to PHP5
29.05.2007