Quantcast

Jump to content


Photo

Proper Loop Etiquette?


  • Please log in to reply
8 replies to this topic

#1 Pyro699

Pyro699
  • 1543 posts


Users Awards

Posted 26 October 2010 - 03:50 AM

Hey,

I have a question and I'm hoping one of you awesome programmers could help me with a problem I've been pondering over :)

Script 1:

while checkForTrue():
continue


Script 2:

import time
while checkForTrue():
time.sleep(0.1)


Now, checkForTrue() is a simple function that will decide at some point weather or not to return true... for the sake of this example its not really important when, i just wanted to throw that in there.

Which script (1 or 2) should you try to use in your every day coding? At first glance you'd guess the second one... because it is not calling checkForTrue() every chance it has but at the same time your running an alternate function (time.sleep(x)) which has its own methods for causing the script to delay a set amount of time.

Ill stop guessing and see what you guys have to say now :)

~Cody Woolaver

#2 Faval

Faval
  • 637 posts

Posted 26 October 2010 - 05:47 AM

I guess it would depend on the situation. I can't really think of many times you would use the second situation though. Unless maybe you were working with forks and threads that would require waits. If you wanted your loop to run infinitely but only on set times, why not just use a timer to call the loop whenever it is time?

What reason would you actually wait to pause and delay the loop exactly?

#3 jcrdude

jcrdude
  • Oh shit there's a thing here

  • 7001 posts


Users Awards

Posted 26 October 2010 - 06:58 AM

Infinte loops are a pain in the ass. I would use the first one, but make sure there's some sort of emergency break in the loop because that shit will craaaaaash without ever giving you a true syntax error.

#4 Faval

Faval
  • 637 posts

Posted 26 October 2010 - 07:02 AM

Infinte loops are a pain in the ass. I would use the first one, but make sure there's some sort of emergency break in the loop because that shit will craaaaaash without ever giving you a true syntax error.


I don't know if that is what he's implying since it says while checkfortrue(), he's calling some function to return true/false so there's probably some checking going on.

#5 Pyro699

Pyro699
  • 1543 posts


Users Awards

Posted 26 October 2010 - 07:29 AM

Remember the hypothetical part of this thread xD

And Faval is right... but it was more or less the number of times the machine was polled using the 2 options i guess xP I suppose that it is completely situation dependent...

#6 jcrdude

jcrdude
  • Oh shit there's a thing here

  • 7001 posts


Users Awards

Posted 26 October 2010 - 07:49 AM

I don't know if that is what he's implying since it says while checkfortrue(), he's calling some function to return true/false so there's probably some checking going on.



Right, but there's no guarantee checkfortrue() will ever change values. I've made the mistake before and it's a pain in the ass :p

Remember the hypothetical part of this thread xD

And Faval is right... but it was more or less the number of times the machine was polled using the 2 options i guess xP I suppose that it is completely situation dependent...


I still prefer #1 either way... was just warning to keep an eye out for infinite loops. They're sometimes hard to diagnose as they don't produce a true syntax error.

#7 Faval

Faval
  • 637 posts

Posted 26 October 2010 - 08:45 AM

Remember the hypothetical part of this thread xD

And Faval is right... but it was more or less the number of times the machine was polled using the 2 options i guess xP I suppose that it is completely situation dependent...


It's going to be polled the same number of times anyway, the only difference is you're delaying it by x number of milliseconds. Unless the checkfortrue() function has something that will change over time?

There isn't much of a reason to put a wait, unless you're working with threads/forks/semaphore and they all happen to be sharing some resources and have to wait till one fork/thread is done with it. But that's a whole other monster of it's own.

And this is hypothetical so i just assume checkfortrue() works and doesn't lead to an infinite loop :p

#8 Kway

Kway
  • Proud to be a Brony

  • 1242 posts


Users Awards

Posted 26 October 2010 - 10:01 AM

Script 1:


while checkForTrue():
continue


Script 2:

import time
while checkForTrue():
time.sleep(0.1)


It depends on what you are doing. Everybody uses Script 1 (even Script 2 does Posted Image). The only time you need the loop to wait before another iteration is if it needs to wait for the variables of checkForTrue() to update in another thread or something. Also you should never use an empty loop like Script 1 because it will use up all of the CPU's cycles (try it yourself) so the minimum you should have there is 1 command so the loop actually does something to get closer to exiting or prevent checking thousands of times a second.

#9 Noitidart

Noitidart
  • Neocodex Co-Founder

  • 23214 posts


Users Awards

Posted 26 October 2010 - 11:50 AM

If the checkForTrue function is waiting for a user response rather the do a loop i would add an event listener. If you're waiting for a certain time i would take the now time and the then time get the diff and do the wait(diff). I havent ever used an infinite loop or even a loop to wait for a response. I do though when it im doing other stuff, but of course its not infinite.


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users