|
Joined: Jan 2007
Posts: 1,156
Hoopy frood
|
OP
Hoopy frood
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. 
|
|
|
|
Joined: Oct 2003
Posts: 313
Fjord artisan
|
Fjord artisan
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  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
|
|
|
|
Joined: Jan 2007
Posts: 1,156
Hoopy frood
|
OP
Hoopy frood
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!!
|
|
|
|
Joined: Sep 2005
Posts: 2,881
Hoopy frood
|
Hoopy frood
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.
|
|
|
|
Joined: Oct 2004
Posts: 8,330
Hoopy frood
|
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
|
|
|
|
Joined: Oct 2003
Posts: 313
Fjord artisan
|
Fjord artisan
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)
;; 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).
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
|
|
|
|
Joined: Feb 2003
Posts: 810
Hoopy frood
|
Hoopy frood
Joined: Feb 2003
Posts: 810 |
pheonix = me in my stubborn and ill informed days  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  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
|
|
|
|
Joined: Oct 2004
Posts: 8,330
Hoopy frood
|
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. 
Invision Support #Invision on irc.irchighway.net
|
|
|
|
Joined: Jan 2007
Posts: 1,156
Hoopy frood
|
OP
Hoopy frood
Joined: Jan 2007
Posts: 1,156 |
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>
|
|
|
|
Joined: Oct 2004
Posts: 8,330
Hoopy frood
|
Hoopy frood
Joined: Oct 2004
Posts: 8,330 |
That's what BREAK is for. 
Invision Support #Invision on irc.irchighway.net
|
|
|
|
Joined: Apr 2004
Posts: 759
Hoopy frood
|
Hoopy frood
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
|
|
|
|
Joined: Sep 2005
Posts: 2,881
Hoopy frood
|
Hoopy frood
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 
|
|
|
|
Joined: Oct 2003
Posts: 313
Fjord artisan
|
Fjord artisan
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:
;;[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.
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
|
|
|
|
Joined: Apr 2004
Posts: 759
Hoopy frood
|
Hoopy frood
Joined: Apr 2004
Posts: 759 |
They still both dont do the same thing which isnt really a fair comparison IMO :P
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
}
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
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
}
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: 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
|
|
|
|
Joined: Oct 2003
Posts: 313
Fjord artisan
|
Fjord artisan
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  - 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. 
Sais
|
|
|
|
Joined: Apr 2004
Posts: 759
Hoopy frood
|
Hoopy frood
Joined: Apr 2004
Posts: 759 |
No breaks on Planes ? ! 
$maybe
|
|
|
|
Joined: Oct 2003
Posts: 313
Fjord artisan
|
Fjord artisan
Joined: Oct 2003
Posts: 313 |
Only when the pilot has to use the head.
Sais
|
|
|
|
Joined: Oct 2005
Posts: 1,741
Hoopy frood
|
Hoopy frood
Joined: Oct 2005
Posts: 1,741 |
No breaks.. but there sure are MF snakes on the MF planes. :P
-genius_at_work
|
|
|
|
|