|
| 26 Aug 2014 11:23 PM |
Which is more efficient? I already know that using break on an infinite for loop is less efficient than both of them.
"i wan't 2 make a game and be rich like the creaiter of tmm but i don't no how 2 use scripts so liek, maik dis stoofs 4 me n i wil give u tix kthx" |
|
|
| Report Abuse |
|
|
dans59
|
  |
| Joined: 15 Apr 2011 |
| Total Posts: 8606 |
|
|
| 26 Aug 2014 11:29 PM |
Depends on what you're doing. But most of the time, I do While.
RAA OR DIE |
|
|
| Report Abuse |
|
|
|
| 26 Aug 2014 11:33 PM |
I'm asking this regardless of whatever may be inside the loop.
I want to know if I ran a loop, even only once, which would cause (even a fraction of a second) less lag.
"i wan't 2 make a game and be rich like the creaiter of tmm but i don't no how 2 use scripts so liek, maik dis stoofs 4 me n i wil give u tix kthx" |
|
|
| Report Abuse |
|
|
|
| 27 Aug 2014 12:51 AM |
| They are both equally efficient/quick, except repeat is actually very slightly faster in the beginning because it takes less syntax parsing to read. |
|
|
| Report Abuse |
|
|
oxcool1
|
  |
| Joined: 05 Nov 2009 |
| Total Posts: 15444 |
|
|
| 27 Aug 2014 01:04 AM |
while statement do -- stuff end
1- checks statement if true, if false loop stops 2- does 'stuff' 3- repeats 1
repeat -stuff until statement
1- does 'stuff' 2- checks if statement is false, if true loop stops 3- repeats 1
Use while if you want to check for the statement first before doing 'stuff'. Repeat if you want to do stuff before checking for the statement. |
|
|
| Report Abuse |
|
|
|
| 27 Aug 2014 12:34 PM |
I always prefer to use repeat loops myself because when you're doing infinite loops you can do:
repeat block until nil
Instead of:
while true do block end
Being a fast typist myself, it's not that big of a deal, but repeat until nil is still easier to type out than while true do end. More efficient to write out, at least.
By the way oxford, I wasn't asking for an explanation of both of them -- I knew their effects and that they could both be used for the exact same reasons, I just wanted to know if using one was more efficient than the other.
Thanks dogwarrior, I'll remember that. |
|
|
| Report Abuse |
|
|
blockoo
|
  |
| Joined: 08 Nov 2007 |
| Total Posts: 17202 |
|
|
| 27 Aug 2014 01:30 PM |
@Immacularity I find it uglier. |
|
|
| Report Abuse |
|
|
lah30303
|
  |
| Joined: 15 Feb 2008 |
| Total Posts: 10027 |
|
|
| 27 Aug 2014 02:29 PM |
Repeat is slightly faster if the loop runs at least once, because both loops will have to check their condition after they finish doing stuff but repeat doesn't need to when entering the loop, so it will do one less check. If you don't want the loop to run the first time through regardless of the condition, however, you can't use repeat until. The miniscule gain in speed is pointless unless it's nested in loops or the condition check is slow (such as calling a function that will return either true or false, and the function is very slow). |
|
|
| Report Abuse |
|
|
lah30303
|
  |
| Joined: 15 Feb 2008 |
| Total Posts: 10027 |
|
|
| 27 Aug 2014 02:33 PM |
| Also gaining a "fraction of a second" would be an overstatement if you're thinking of a fraction of a second the way people normally think of a fraction of a second (though it's technically true, technicalities would make "fraction of a second" hold little meaning). |
|
|
| Report Abuse |
|
|
mutedmic
|
  |
| Joined: 26 Aug 2014 |
| Total Posts: 12 |
|
|
| 27 Aug 2014 03:15 PM |
It really just depends on what you're doing.
For example, Say I wanted my script to do something until a condition was reached. Which is more efficient?
repeat code wait() until condition
while wait() do if condition then break end end
Obviously repeat. If we used while for this sort of problem we would've already used 2 scopes and overall in my opinion it's highly inefficient for your problem. |
|
|
| Report Abuse |
|
|
|
| 27 Aug 2014 04:13 PM |
Or you could just try not being an idiot and realizing how while loops work.
while not condition do wait() end repeat wait() until condition |
|
|
| Report Abuse |
|
|
mutedmic
|
  |
| Joined: 26 Aug 2014 |
| Total Posts: 12 |
|
|
| 28 Aug 2014 10:26 AM |
| It's a bit childish to call someone an idiot over a mistake. |
|
|
| Report Abuse |
|
|
digpoe
|
  |
| Joined: 02 Nov 2008 |
| Total Posts: 9092 |
|
|
| 28 Aug 2014 12:15 PM |
| You should only use repeat if you need to run the code at least once. |
|
|
| Report Abuse |
|
|
Alyte
|
  |
| Joined: 24 Oct 2011 |
| Total Posts: 10090 |
|
| |
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 28 Aug 2014 06:43 PM |
while is not faster.
repeat is a do-while loop while is a while-do loop
There should be 0 speed difference (except in the case the statement is false, where repeat will actually do something and making is slower (depending what you are doing)) |
|
|
| Report Abuse |
|
|
lah30303
|
  |
| Joined: 15 Feb 2008 |
| Total Posts: 10027 |
|
|
| 29 Aug 2014 10:25 AM |
| If n is the number of times the loop runs, a while loop will check its condition n + 1 times, and a repeat loop will check it n times. The repeat loop will always run at least once, the while loop can run zero times. The OP said the loop will always run at least once, so the zero case does not matter. n + 1 > n, so the while loop will make more operations. |
|
|
| Report Abuse |
|
|