1

I am making a program that uses a loop to run forever and I have a snippet of code below to show you how I acheive the loop. This is just an example and not the actual program. It is the same idea though.

import time
def start():
    print "hello"
    time.sleep(0.2)
    start()
start()

All of my programmer friends tell me not to do this, and use a while loop instead. Like this:

import time
def start():
    while True:
        print "Hello"
        time.sleep(0.2)
start()

Why should I use the while loop instead when both methods work perfectly fine?

5
  • 7
    Does the recursive method actually "work fine"? Don't you get an error about max recursion depth rather quickly? Commented Apr 5, 2014 at 2:58
  • I have a sneaky suspicion that neither approach is better, but there is a third approach involving lazy evaluation that can only be speculated upon until we have more of the code. #justsaying Commented Apr 5, 2014 at 3:04
  • @kojiro isn't that just complicating things? he merely 'seems' to want to run an infinite loop with some sleep (as a throttling parameter). I don't get why lazy eval is required in this scenario? Commented Apr 5, 2014 at 3:06
  • @SrikarAppal I could be off base. It's just a suspicion. Commented Apr 5, 2014 at 3:08
  • 2
    Your first approach might be a viable option in a truly functional language where tail call optimizations are performed. Since python neither is a functional language nor does tail call optimizations, it will fail. Had the optimization been done, both approaches would be more or less equivalent. Commented Apr 5, 2014 at 3:12

4 Answers 4

6

Each time you are recursing, you are pushing a frame context onto your program stack. Soon you would have used up your entire allotted stack space causing stackoverflow, no pun intended ;)

The second approach has no such flaws. Hence by the looks of it 2nd is better than 1st approach (unless more of the program is presented).

Sign up to request clarification or add additional context in comments.

1 Comment

The pun is certainly intended, just not by you. :P
2

If you run the program continuously with the recursion, you will get RuntimeError:

Traceback (most recent call last):
  File "t.py", line 8, in <module>
    start()
  File "t.py", line 7, in start
    start()
  File "t.py", line 7, in start
    start()
  ...
  File "t.py", line 7, in start
    start()
RuntimeError: maximum recursion depth exceeded

>>> import sys
>>> sys.getrecursionlimit()
1000

2 Comments

You could also set the recursion limit higher to with sys.setrecursionlimit()
not a good idea to inc. recursion limit. since you are running this indefinitely, you will sooner or rather hit the limit, causing program crash...
1

the while loop amd recursion both have their advantages and disadvantages but while loop is just a test and conditional jump. wheras ecursion involves pushing a stack frame, jumping, returning, and popping back from the stack.So they might have preferred while loop :)

1 Comment

ofcourse, testing and jumping would be more fast when compared to the steps involved in recursion.So it will be less CPU intensive
1

Python does not have Tail Call Optimization (TCO) and cannot elide the stack. As such, recursion should not be used for an unbound depth (in languages without TCO) lest you get a stack-overflow error!

While the current code might appear to be fine, consider this simple change which will quickly reveal a problem.

def start():
    print "hello"
    # let's go faster! -- time.sleep(0.2)
    start()
start()

6 Comments

I see no difference with the exception of the comment. What is the change?
@TheNotGoodAtCodeGuy the comment.
@TheNotGoodAtCodeGuy It runs much faster - that is, instead of only recursing 5 times a second, it might recurse 50000 times per second (and will very quickly fail).
See, that is why I have the time.sleep(), first off, I have a slow computer, so it uses less processing power, and it doesn't cause immediate stack overflow and or crash my computer.
@TheNotGoodAtCodeGuy Recursion should not be used for an unbound depth as shown in this case. The sleep doesn't matter and having it only hides the problem with this approach.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.