Speed of do while

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marco

    Speed of do while

    We can read this comment at php.net
    [color=blue]
    >jon
    >05-Nov-2003 10:06
    >According to my tests, the "do...while " control structure actually seems to[/color]
    be ~40% faster than the "for" control structure. At least using PHP v4.0.6[color=blue]
    >on a few fairly modern Linux machines.[/color]
    [color=blue]
    >I used a the getmicrotime function as defined at[/color]
    Return current Unix timestamp with microseconds

    [color=blue]
    >I tested the following 5 scenarios, including two different ways of calling[/color]
    a "for" loop. The less ordinary way to call "for", I'll call "altfor".
    Here >are the code scenarios for different ways from counting from
    0->($num-1), in my case $num = 10000:
    [color=blue]
    >// 'altfor':
    > $a=0;
    > for (; $a<$num;) { $a++;}[/color]
    [color=blue]
    >// 'for':
    > for ($a=0; $a<$num; $a++) {}[/color]
    [color=blue]
    >// 'foreach':
    > foreach (range(0,$num-1) as $a ) {}[/color]
    [color=blue]
    >// 'while':
    > $a = 0;
    > while ($a < $num) {$a++;}[/color]
    [color=blue]
    >// 'do':
    > $a = 0;
    > do {} while (++$a < $num);[/color]
    [color=blue]
    >Each scenario is called in random order, and here is a typical result:[/color]
    [color=blue]
    >Average times for 100 iterations of each loop: altfor, do, for, foreach,[/color]
    while.[color=blue]
    >All loops are incremented from 0 to 9999.[/color]
    [color=blue]
    > altfor: 0.0368828332424 16 seconds, 138.25 percent of ~ 0.0267 seconds
    > do: 0.0266782820224 76 seconds, 100.00 percent of ~ 0.0267 seconds
    > for: 0.0373089981079 10 seconds, 139.85 percent of ~ 0.0267 seconds
    >foreach: 0.0648571395874 02 seconds, 243.11 percent of ~ 0.0267 seconds
    > while: 0.0365088176727 29 seconds, 136.85 percent of ~ 0.0267 seconds
    >
    >...so to be clear, I ran "for", et all, from 0->9999. This was done 100[/color]
    times. Above are the average times for those 100 tests of loops of 10,000.
    I >have run my script multiple time on several different Linux systems here
    at my work. All w/ similar results...

    As anyone tryed to see if thats true and do...while is about 40% faster than
    for ?














  • CountScubula

    #2
    Re: Speed of do while

    Just curious, but put some real calculations or procedures within the loops,
    perhaps read a file, process an array or combo therof.

    The reason is, it might not be 40% faster, but rather a small time frame of
    less overhead. run some calculations that take 1 second to run, and put that
    in the loop 10 times, thus roughly 10 seconds, then you might see some loops
    running at 9.8 or 10.1, but not 10 sec and 6 sec (40% faster)

    --
    Mike Bradley
    http://www.gzentools.com -- free online php tools

    "Marco" <mpgtlatbluewin dotch> wrote in message
    news:403e2ce3$1 _2@news.bluewin .ch...[color=blue]
    > We can read this comment at php.net
    >[color=green]
    > >jon
    > >05-Nov-2003 10:06
    > >According to my tests, the "do...while " control structure actually seems[/color][/color]
    to[color=blue]
    > be ~40% faster than the "for" control structure. At least using PHP[/color]
    v4.0.6[color=blue][color=green]
    > >on a few fairly modern Linux machines.[/color]
    >[color=green]
    > >I used a the getmicrotime function as defined at[/color]
    > http://www.php.net/microtime
    >[color=green]
    > >I tested the following 5 scenarios, including two different ways of[/color][/color]
    calling[color=blue]
    > a "for" loop. The less ordinary way to call "for", I'll call "altfor".
    > Here >are the code scenarios for different ways from counting from
    > 0->($num-1), in my case $num = 10000:
    >[color=green]
    > >// 'altfor':
    > > $a=0;
    > > for (; $a<$num;) { $a++;}[/color]
    >[color=green]
    > >// 'for':
    > > for ($a=0; $a<$num; $a++) {}[/color]
    >[color=green]
    > >// 'foreach':
    > > foreach (range(0,$num-1) as $a ) {}[/color]
    >[color=green]
    > >// 'while':
    > > $a = 0;
    > > while ($a < $num) {$a++;}[/color]
    >[color=green]
    > >// 'do':
    > > $a = 0;
    > > do {} while (++$a < $num);[/color]
    >[color=green]
    > >Each scenario is called in random order, and here is a typical result:[/color]
    >[color=green]
    > >Average times for 100 iterations of each loop: altfor, do, for, foreach,[/color]
    > while.[color=green]
    > >All loops are incremented from 0 to 9999.[/color]
    >[color=green]
    > > altfor: 0.0368828332424 16 seconds, 138.25 percent of ~ 0.0267 seconds
    > > do: 0.0266782820224 76 seconds, 100.00 percent of ~ 0.0267 seconds
    > > for: 0.0373089981079 10 seconds, 139.85 percent of ~ 0.0267 seconds
    > >foreach: 0.0648571395874 02 seconds, 243.11 percent of ~ 0.0267 seconds
    > > while: 0.0365088176727 29 seconds, 136.85 percent of ~ 0.0267 seconds
    > >
    > >...so to be clear, I ran "for", et all, from 0->9999. This was done 100[/color]
    > times. Above are the average times for those 100 tests of loops of[/color]
    10,000.[color=blue]
    > I >have run my script multiple time on several different Linux systems[/color]
    here[color=blue]
    > at my work. All w/ similar results...
    >
    > As anyone tryed to see if thats true and do...while is about 40% faster[/color]
    than[color=blue]
    > for ?
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >[/color]


    Comment

    • Chung Leong

      #3
      Re: Speed of do while

      This has been the case since PHP3. Does it matter? Probably not.

      Functions with longer names are also slower, in case you're wondering. Dito
      for long variable names.

      Uzytkownik "Marco" <mpgtlatbluewin dotch> napisal w wiadomosci
      news:403e2ce3$1 _2@news.bluewin .ch...[color=blue]
      > We can read this comment at php.net
      >[color=green]
      > >jon
      > >05-Nov-2003 10:06
      > >According to my tests, the "do...while " control structure actually seems[/color][/color]
      to[color=blue]
      > be ~40% faster than the "for" control structure. At least using PHP[/color]
      v4.0.6[color=blue][color=green]
      > >on a few fairly modern Linux machines.[/color]
      >[color=green]
      > >I used a the getmicrotime function as defined at[/color]
      > http://www.php.net/microtime
      >[color=green]
      > >I tested the following 5 scenarios, including two different ways of[/color][/color]
      calling[color=blue]
      > a "for" loop. The less ordinary way to call "for", I'll call "altfor".
      > Here >are the code scenarios for different ways from counting from
      > 0->($num-1), in my case $num = 10000:
      >[color=green]
      > >// 'altfor':
      > > $a=0;
      > > for (; $a<$num;) { $a++;}[/color]
      >[color=green]
      > >// 'for':
      > > for ($a=0; $a<$num; $a++) {}[/color]
      >[color=green]
      > >// 'foreach':
      > > foreach (range(0,$num-1) as $a ) {}[/color]
      >[color=green]
      > >// 'while':
      > > $a = 0;
      > > while ($a < $num) {$a++;}[/color]
      >[color=green]
      > >// 'do':
      > > $a = 0;
      > > do {} while (++$a < $num);[/color]
      >[color=green]
      > >Each scenario is called in random order, and here is a typical result:[/color]
      >[color=green]
      > >Average times for 100 iterations of each loop: altfor, do, for, foreach,[/color]
      > while.[color=green]
      > >All loops are incremented from 0 to 9999.[/color]
      >[color=green]
      > > altfor: 0.0368828332424 16 seconds, 138.25 percent of ~ 0.0267 seconds
      > > do: 0.0266782820224 76 seconds, 100.00 percent of ~ 0.0267 seconds
      > > for: 0.0373089981079 10 seconds, 139.85 percent of ~ 0.0267 seconds
      > >foreach: 0.0648571395874 02 seconds, 243.11 percent of ~ 0.0267 seconds
      > > while: 0.0365088176727 29 seconds, 136.85 percent of ~ 0.0267 seconds
      > >
      > >...so to be clear, I ran "for", et all, from 0->9999. This was done 100[/color]
      > times. Above are the average times for those 100 tests of loops of[/color]
      10,000.[color=blue]
      > I >have run my script multiple time on several different Linux systems[/color]
      here[color=blue]
      > at my work. All w/ similar results...
      >
      > As anyone tryed to see if thats true and do...while is about 40% faster[/color]
      than[color=blue]
      > for ?
      >
      >
      >
      >
      >
      >
      >
      >
      >
      >
      >
      >
      >
      >[/color]


      Comment

      Working...