mIRC Home    About    Download    Register    News    Help

Print Thread
LOOPS: while vs. goto #168169 04/01/07 08:26 PM
Joined: Jan 2007
Posts: 1,156
D
DJ_Sol Offline OP
Hoopy frood
OP Offline
Hoopy frood
D
Joined: Jan 2007
Posts: 1,156
Hey all!! I am asking for opinions and would prefer proof. Friends say goto loops are faster, more stable than a while loop. I was curious what everyone thought about that. What do you think is better and why? Is one better than the other? Thanks for your replies in advance. smile

Re: LOOPS: while vs. goto [Re: DJ_Sol] #168170 04/01/07 08:35 PM
Joined: Oct 2003
Posts: 313
S
Sais Offline
Fjord artisan
Offline
Fjord artisan
S
Joined: Oct 2003
Posts: 313
My major problem with goto is that it can be so easy to misuse, especially if you aren't fully aware of what you are doing. A while loop has a well-defined structure which makes it much less open to such 'abuse'.

Somewhere I have a monologue on the evils of goto...I'll dig it out and post it smile

I would, though be interested in seeing if there is a speed difference between a while and an equivalent goto loop. I can't imagine there's much in it, though.


Sais
Re: LOOPS: while vs. goto [Re: Sais] #168172 04/01/07 09:34 PM
Joined: Sep 2005
Posts: 2,881
H
hixxy Offline
Hoopy frood
Offline
Hoopy frood
H
Joined: Sep 2005
Posts: 2,881
http://forums.mirc.com/ubbthreads.php?ub...=true#Post49540 - if you read from there onwards there are points made and benchmarks posted.

pheonix = me in my stubborn and ill informed days blush LocutusofBorg is right.

Re: LOOPS: while vs. goto [Re: DJ_Sol] #168173 04/01/07 09:57 PM
Joined: Jan 2007
Posts: 1,156
D
DJ_Sol Offline OP
Hoopy frood
OP Offline
Hoopy frood
D
Joined: Jan 2007
Posts: 1,156
Thanks for the replies. For the purpose of this thread, lets assume the scripter uses both goto and while loops correctly, lol. :p

EDIT: Thanks Hixxy I read that post... and uhh... well nice to see the codes tested against each other.

Thanks for the help!! and everyone else, by all means, include your opinions!!

Re: LOOPS: while vs. goto [Re: DJ_Sol] #168174 04/01/07 10:02 PM
Joined: Sep 2005
Posts: 2,881
H
hixxy Offline
Hoopy frood
Offline
Hoopy frood
H
Joined: Sep 2005
Posts: 2,881
While loops are faster, neater and a more correct method - if that makes sense. Goto labels are not supposed to be used as a looping mechanism, but they can be.

Re: LOOPS: while vs. goto [Re: DJ_Sol] #168178 04/01/07 10:50 PM
Joined: Oct 2004
Posts: 8,330
Riamus2 Offline
Hoopy frood
Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,330
Besides the tests showing that WHILE is faster, it has LONG been considered BAD to use GOTO for looping in any programming language, except in specific situations. There are some situations where having a GOTO is useful and makes more sense than a WHILE loop. However, those are rare. Use WHILE and you'll be better off.


Invision Support
#Invision on irc.irchighway.net
Re: LOOPS: while vs. goto [Re: hixxy] #168181 04/01/07 11:08 PM
Joined: Oct 2003
Posts: 313
S
Sais Offline
Fjord artisan
Offline
Fjord artisan
S
Joined: Oct 2003
Posts: 313
I modified the test a little.

The place where I believed gotos were better than while loops are when you need to break out of the loop somewhere in the middle (wtest2 and wtest3 versus gtest1).

I also compared the shorter form against the longer form of the while (wtest versus wtest1) and against the equivalent goto (as in the original test) (gtest)

Code:
;; Just to tidy the results up a bit
alias -l trim return $left($base($calc($$1 / 1000),10,10,1,3) $+ 000,5)

alias looptest {
;;echo -a while: [5000runs, basic] [5000runs, short form] [9000runs, flagged at 5000] [9000runs, break'd at 5000] ~ goto: [5000runs, basic] [9000runs, goto end'd at 5000]

echo -a while: $trim($wtest) $trim($wtest1) $trim($wtest2) $trim($wtest3) ~ goto: $trim($gtest) $trim($gtest1)
}

;; the tests
;; [50000runs, basic]
alias wtest {
  var %ticks = $ticks,%i = 0
  while (%i != 50000) {
    inc %i
  }
  return $calc(($ticks - %ticks))
}
;;[50000runs, short form]
alias wtest1 {
  var %ticks = $ticks,%i = 0
  while (%i != 50000) inc %i
  return $calc(($ticks - %ticks))
}
;;[90000runs, flagged at 50000]
alias wtest2 {
  var %ticks = $ticks,%i = 0
  var %flag = $true;
  while (%i != 90000) {
    if (%i == 50000) { %flag = $true }
    inc %i
  }
  return $calc(($ticks - %ticks))
}
alias wtest3 {
  var %ticks = $ticks,%i = 0
  while (%i != 90000) {
    if (%i == 50000) { break }
    inc %i
  }
  return $calc(($ticks - %ticks))
}

;; The GOTO tests
;; [50000runs, basic]
alias gtest {
  var %ticks = $ticks,%i = 0
  :start
  if (%i <= 50000) {
    inc %i
    goto start
  }
  return $calc(($ticks - %ticks))
}
;;[90000runs, goto end'd at 50000]
alias gtest1 {
  var %ticks = $ticks,%i = 0
  :start
  if (%i <= 90000) {
    inc %i
    if (%i == 50000) { goto end }
    goto start
  }
  :end
  return $calc(($ticks - %ticks))
}


This consistently gives a better result for gotos in wtest2 versus gtest1, but very slightly faster (less than 1/10th sec over 90000 runs) for wtest3 versus gtest1.

Over 90000 runs the shorter form is almost 1/10th of a second faster than the long form (wtest1 versus wtest), and 2~3/10ths of a second faster than the goto (gtest).

Code:
       wtest wtest1 wtest2 wtest3        gtest gtest1 
while: 0.953 0.858  2.858  1.500 ~ goto: 1.093 1.672
while: 0.952 0.858  2.843  1.500 ~ goto: 1.093 1.672
while: 0.968 0.858  2.843  1.515 ~ goto: 1.093 1.672
while: 0.952 0.875  2.843  1.500 ~ goto: 1.109 1.656
while: 0.953 0.858  2.858  1.500 ~ goto: 1.093 1.702
while: 0.968 0.875  2.843  1.500 ~ goto: 1.109 1.687
while: 0.952 0.858  2.858  1.516 ~ goto: 1.093 1.702
while: 0.968 0.875  2.890  1.500 ~ goto: 1.110 1.702
while: 0.952 0.875  2.906  1.516 ~ goto: 1.140 1.672
while: 0.968 0.858  2.907  1.515 ~ goto: 1.110 1.687


Sais
Re: LOOPS: while vs. goto [Re: hixxy] #168189 05/01/07 12:05 AM
Joined: Feb 2003
Posts: 810
C
cold Offline
Hoopy frood
Offline
Hoopy frood
C
Joined: Feb 2003
Posts: 810
Originally Posted By: hixxy
pheonix = me in my stubborn and ill informed days blush LocutusofBorg is right.


To me, the interesting part is that your signature as pheonix is "new username: tidy_trax", and now you're hixxy. You've been changing nicks a lot, didn't you grin

Edit: tidy_trax's signature is "New username: hixxy". I thought there was more, heheh


About goto/while, there was some discussion about them at mircscripts.org as well. There was much more in the past, but I guess the webboard data is lost.

Last edited by cold; 05/01/07 12:09 AM.

* cold edits his posts 24/7
Re: LOOPS: while vs. goto [Re: cold] #168195 05/01/07 12:26 AM
Joined: Oct 2004
Posts: 8,330
Riamus2 Offline
Hoopy frood
Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,330
Lol, I didn't realize tidy_trax and hixxy were the same person. I don't think I ever knew the Phoenix nick. smile


Invision Support
#Invision on irc.irchighway.net
Re: LOOPS: while vs. goto [Re: Sais] #168206 05/01/07 08:17 AM
Joined: Jan 2007
Posts: 1,156
D
DJ_Sol Offline OP
Hoopy frood
OP Offline
Hoopy frood
D
Joined: Jan 2007
Posts: 1,156
Quote:
The place where I believed gotos were better than while loops are when you need to break out of the loop somewhere in the middle (wtest2 and wtest3 versus gtest1).



Well I have actually used goto in while loops for this.

while (%x <= %x_i) {
if (comparison) { command | goto fin }
inc %x_i | if (%x_i > %x) goto fin
}
:fin <compile and halt>

Re: LOOPS: while vs. goto [Re: DJ_Sol] #168222 05/01/07 02:26 PM
Joined: Oct 2004
Posts: 8,330
Riamus2 Offline
Hoopy frood
Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,330
That's what BREAK is for. smile


Invision Support
#Invision on irc.irchighway.net
Re: LOOPS: while vs. goto [Re: Sais] #168226 05/01/07 03:51 PM
Joined: Apr 2004
Posts: 759
M
Mpdreamz Offline
Hoopy frood
Offline
Hoopy frood
M
Joined: Apr 2004
Posts: 759
wtest 2 would be slower yeah its making 9000 iterations (Noone will credit me for using that term these days haha :p) where as gtest1 is making 5000. Not really a fair comparison if you ask me :P

wtest3 against goto1 basically proves goto is inferior in all cases :P


$maybe
Re: LOOPS: while vs. goto [Re: cold] #168228 05/01/07 04:31 PM
Joined: Sep 2005
Posts: 2,881
H
hixxy Offline
Hoopy frood
Offline
Hoopy frood
H
Joined: Sep 2005
Posts: 2,881
I've had about 10, but only 3 on here and probably only 3 people will remember me by.

But yeah, more than most laugh

Re: LOOPS: while vs. goto [Re: Mpdreamz] #168262 06/01/07 02:26 AM
Joined: Oct 2003
Posts: 313
S
Sais Offline
Fjord artisan
Offline
Fjord artisan
S
Joined: Oct 2003
Posts: 313
oops. Bit of a booboo there. I neither tested the flag, nor set it correctly. Must try harder.

It *should* have been:
Code:
;;[90000runs, flagged at 50000]
alias wtest2 {
  var %ticks = $ticks,%i = 0
  var %flag = $true
  while (%i != 90000 && %flag) {
    if (%i == 50000) { %flag = $false }
    inc %i
  }
  return $calc(($ticks - %ticks))
}


This is still giving slower times for while over goto. I think it's to be expected - setting a flag means that it has to iterate over the loop one extra time and test both the condition and then the flag before it exits the loop.

Code:
       wtest2        gtest1
while: 1.734 ~ goto: 1.687
while: 1.812 ~ goto: 1.702
while: 1.812 ~ goto: 1.687
while: 1.828 ~ goto: 1.733
while: 1.812 ~ goto: 1.718
while: 1.827 ~ goto: 1.687
while: 1.797 ~ goto: 1.702
while: 1.797 ~ goto: 1.702
while: 1.827 ~ goto: 1.687
while: 1.812 ~ goto: 1.687

Last edited by Sais; 06/01/07 02:35 AM.

Sais
Re: LOOPS: while vs. goto [Re: Sais] #168282 06/01/07 10:46 AM
Joined: Apr 2004
Posts: 759
M
Mpdreamz Offline
Hoopy frood
Offline
Hoopy frood
M
Joined: Apr 2004
Posts: 759
They still both dont do the same thing which isnt really a fair comparison IMO :P

Code:
alias wtest5 {
  var %ticks = $ticks,%i = 0,%flag = 1
  while ((%i != 90000) && (%flag)) {
    if (%i == 49999) %flag = 0
    inc %i
  }
  return wtest5: %i $calc(($ticks - %ticks)/1000) flag: %flag
}
alias gtest3 {
  var %ticks = $ticks,%i = 0,%flag = 1
  :start
  if (%i <= 90000) {
    inc %i
    if (%i == 50000) { 
      %flag = 0
      goto end 
    }
    goto start
  }
  :end
  return gtest3: %i $calc(($ticks - %ticks)/1000) flag: %flag
}


Quote:

wtest5: 50000 2.384 flag: 0 gtest3: 50000 2.353 flag: 0
wtest5: 50000 2.383 flag: 0 gtest3: 50000 2.364 flag: 0
wtest5: 50000 2.393 flag: 0 gtest3: 50000 2.354 flag: 0


Result is the same though goto is faster in this situation but only because of a crappy break out method in the while which doesn't really prove goto is better in this situation since while and /break is still faster.

concidering this slightly modified wtest3
Code:
alias wtest3 {
  var %ticks = $ticks,%i = 0,%flag = 1
  while (%i != 90000) {
    if (%i == 50000) { 
      %flag = 0 
      break 
    }
    inc %i
  }
  return wtest3: %i $calc(($ticks - %ticks)/1000) flag: %flag
}


Quote:

wtest3: 50000 2.203 flag: 0 gtest3: 50000 2.363 flag: 0
wtest3: 50000 2.193 flag: 0 gtest3: 50000 2.334 flag: 0
wtest3: 50000 2.213 flag: 0 gtest3: 50000 2.354 flag: 0


To me, this disproves your hypothesis:
Quote:

The place where I believed gotos were better than while loops are when you need to break out of the loop somewhere in the middle (wtest2 and wtest3 versus gtest1).




$maybe
Re: LOOPS: while vs. goto [Re: Mpdreamz] #168288 06/01/07 12:39 PM
Joined: Oct 2003
Posts: 313
S
Sais Offline
Fjord artisan
Offline
Fjord artisan
S
Joined: Oct 2003
Posts: 313
Agreed - you can write a while loop using break (as I had done) which is (very slightly) faster than the equivalent goto. It is still useful to see the distinctions.

Out of habit I tend to write whiles with break-out flags because it was a requirement to never use break in code being written for avionics certification (something to do with deterministic behaviour - never quite agreed with it myself smile - my first real programming job. *shrug*

Based on these results, I'm going to have to try to remember to use /break next time I need something like this. laugh



Sais
Re: LOOPS: while vs. goto [Re: Sais] #168290 06/01/07 01:16 PM
Joined: Apr 2004
Posts: 759
M
Mpdreamz Offline
Hoopy frood
Offline
Hoopy frood
M
Joined: Apr 2004
Posts: 759
No breaks on Planes ? ! shocked wink


$maybe
Re: LOOPS: while vs. goto [Re: Mpdreamz] #168300 06/01/07 05:46 PM
Joined: Oct 2003
Posts: 313
S
Sais Offline
Fjord artisan
Offline
Fjord artisan
S
Joined: Oct 2003
Posts: 313
Only when the pilot has to use the head.


Sais
Re: LOOPS: while vs. goto [Re: Mpdreamz] #168301 06/01/07 05:57 PM
Joined: Oct 2005
Posts: 1,741
G
genius_at_work Offline
Hoopy frood
Offline
Hoopy frood
G
Joined: Oct 2005
Posts: 1,741
No breaks.. but there sure are MF snakes on the MF planes. :P

-genius_at_work