Speed of Loops

This page - by random - chooses a fast or slow loop or a formula. Just press the reload (refresh) button several times until you have seen all variants of it and experienced the different speed.

Java Script permits, similar as C, to combine several steps in one. This permits to write shorter programs and also to get faster execution speed, in partcular by points 2 and 3 of the following four examples. The speed is increased as the browser does in the abbreviated case (fast loop) access the variables inside the loop less often than in the nonabbreviated case (slow loop). Here the four examples.
  1. Variables can receive values directly at the declaration:
               Abbreviation        Stands for
               var x = 0;          var x; x = 0; 
  2. If a variable in an update appears on bosth sides of an assignment, then one can in certain cases save one occurrence of the variable:
               Abbreviation        Stands for
               x += 5;             x = x+5;
               x -= 18;            x = x-18;
               x *= y+z;           x = x*(y+z);
               x /= y+z;           x = x/(y+z); 
    Note that the brackets are necesasry on the right hand side; for example compare the results of the two variables v and w after the loop
               var v=1; var w=1; var u;
               for (u=0;u<9;u++)
                 { v *= u+1; w = w*u+1; }
    at the end of this page.
  3. The operators "++" and "--" add and substract 1 to and from a variable, respectively, after it is read.
               Abbreviation        Stands for
               c++;                c = c+1;
               c--;                c = c-1;
               while(c++<9) { X; } while (c<9) { c=c+1; X; } 
    Note that the name of the programming language "C++" refers to this abrevation indicating that it is obtained by "incrementing" C, that is, by adding the concpets of object-oriented programming among other modifications.
  4. The for-loop can also be interpreted as an abbrevation. Assuming that A, C, D are statements and B is an Boolean expression, the following are equivalent.
               Abbreviation        Stands for
               for (A;B;C) D;      A; while (B) { D; C; } 
    Note that the statement C comes after D and not before. The statement D itself can consist of several statements and so "D; " can actually be "{ D; E; }". Anyway, it is good style to write "{ D; }" instead of "D;" in a for-loop.
Implemented are a formula to compute 0+1+2+...+z, a fast loop and a slow loop to do this. This initial value of z is 25000000. There are three ways to do it: the formula of Gauss, an optimized fast loop and a nonoptimized slow loop. Of course, an algorithmic improvement like the one of Gauss outperforms the code-optimization (fast loop), but nevertheless the code-optimization can be very helpful in cases where such an improved algorithm does not exist.
      Formula:  var y = Math.round(0.5*z*(z+1));


      Fast Loop: var y=0; while(z>=0) { y+=z--; }           


      Slow Loop: var y=0; var x=1; while(x<=z) { y = y+x; x=x+1; }

Note that at the Internet-Explorer makes a page slower after repeatedly pressing buttons. To avoid this effect, you can press refresh in order to reset the speed to the original value each time before you press one of the above buttons.