This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Armitige's profile picture

Which is more ideal? While true do or a startup script with shell.run and os.sleep/reboot?

Started by Armitige, 08 November 2014 - 05:15 PM
Armitige #1
Posted 08 November 2014 - 06:15 PM
Which option is the better out of using a startup script to run your program, use os.sleep and then os.reboot, and using a while loop when you need a program to iterate continuously?
Is it dependent on the function of the program?
What are the pros and cons of each method?
Is there a better method than these options?

I've looked over a number of programs, but my experience with lua is very limited, and I find it hard to follow the code in a lot of the more complex programs that run continuously.
Lyqyd #2
Posted 08 November 2014 - 06:24 PM
Don't reboot unless absolutely necessary. I can't think of any situations in which you'd need to reboot.
Armitige #3
Posted 08 November 2014 - 06:26 PM
Don't reboot unless absolutely necessary. I can't think of any situations in which you'd need to reboot.
Ok, thanks for the quick reply 8)
Bomb Bloke #4
Posted 09 November 2014 - 12:38 AM
As to the reason why rebooting is a bad way to loop, it's because it's slow. The reason for that is because it involves making the computer do a whole heap of work over and over again that really isn't required - it's got to re-run bios.lua, it's got to reload all the default APIs, it's got to re-run the shell then after all that it's got to re-run your startup script. Compared to jumping up a few lines to a top of a "while" loop, that's incredibly inefficient.

Another "bad" looping method you'll see some coders use is shell.run() / os.run(). They might have a script run itself, or they might have a script run another script which in turn then runs the first again. This again involves pulling each script off the drive over and over again, and worse yet, when one script starts another the former stays loaded into RAM until the second finishes - the result is multiple copies of each script building up in memory, until you run out of space and they all crash!

(The exact same problem applies to creating a loop by having functions call themselves over and over.)

So why do you see the above methods used at all? Well, simply put, many scripters don't know about while/for/repeat loops, and use the first option for looping that they become aware of.