This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Quicker sleeping
Started by ShadowDisruptor, 09 December 2014 - 12:57 AMPosted 09 December 2014 - 01:57 AM
I'm working with a project that's going to use a lot of animations. The speed, or sleep(), seems to stop getting quicker at 0.1 and won't go any faster (For example, 0.05). This makes the graphics pretty slow and more annoying than cool. Is there any way to do a sleep that's quicker than 0.1 seconds?
Edited on 09 December 2014 - 12:57 AM
Posted 09 December 2014 - 02:08 AM
You will not be able to go any faster then a minecraft tick which is .05 seconds. Sleep should work when set at 1 tick or .05 seconds.
os.sleep()
os.sleep()
Edited on 09 December 2014 - 01:10 AM
Posted 09 December 2014 - 02:18 AM
Indeed, you can't slice time any smaller than a twentieth of a second (and even that assumes that you aren't running lots of other computers at the same time, which'll slow things down further). Be it via timer events (as used by sleep) or via os.clock(), increments of 0.05 seconds are as good as it gets.
If you don't want to "pause" so much as "yield", then you can always queue then immediately pull an event.
If you don't want to "pause" so much as "yield", then you can always queue then immediately pull an event.
Posted 09 December 2014 - 03:12 AM
This explains why no one really does animations. They're painfully slow.You will not be able to go any faster then a minecraft tick which is .05 seconds. Sleep should work when set at 1 tick or .05 seconds.
os.sleep()
If this dosen't load the animations any quicker, then I'm not interested. Thanks though.Indeed, you can't slice time any smaller than a twentieth of a second (and even that assumes that you aren't running lots of other computers at the same time, which'll slow things down further). Be it via timer events (as used by sleep) or via os.clock(), increments of 0.05 seconds are as good as it gets.
If you don't want to "pause" so much as "yield", then you can always queue then immediately pull an event.
Posted 09 December 2014 - 03:36 AM
I've found with intensive animations, depending on how much calculation is done over pure drawing, that you can pretty much just let it go as fast as possible without any delays.
For example, in this animation in OneOS to keep it fast and smooth there's not a single sleep or yield call. It calculates what the preview should look like and just draws it. I haven't really found a case where it's too fast, it is a reasonably intensive draw process.
Now, I'm not really sure how intensive your animation is. I doubt it'll take long that that one because everything being run through a buffer and it's a somewhat complicated drawing process to shrink the images. I'd stick with sleep(0) or see how this works.
You could (although shouldn't) put a for loop that does math.sqrt tons of times or something. I've found also making animations larger than 5 steps just doesn't work.
For example, in this animation in OneOS to keep it fast and smooth there's not a single sleep or yield call. It calculates what the preview should look like and just draws it. I haven't really found a case where it's too fast, it is a reasonably intensive draw process.
Now, I'm not really sure how intensive your animation is. I doubt it'll take long that that one because everything being run through a buffer and it's a somewhat complicated drawing process to shrink the images. I'd stick with sleep(0) or see how this works.
You could (although shouldn't) put a for loop that does math.sqrt tons of times or something. I've found also making animations larger than 5 steps just doesn't work.
Edited on 09 December 2014 - 02:37 AM
Posted 09 December 2014 - 06:35 AM
-snip-
Surely there must be a yield in there somewhere, even if you're not calling it yourself. Otherwise an animation would pause all other computers for the duration of the animation, wouldn't it?
Posted 09 December 2014 - 04:46 PM
I think he's saying that for the animation itself, since it shouldn't ever take more than 10 seconds, it doesn't yield until AFTER it's done, so that it runs through quickly.Surely there must be a yield in there somewhere, even if you're not calling it yourself. Otherwise an animation would pause all other computers for the duration of the animation, wouldn't it?
This was towards his own.
Otherwise for a lot longer animation, there will need to be yielding yeah.
Edited on 09 December 2014 - 03:47 PM
Posted 09 December 2014 - 08:23 PM
Well have you tried doing this?
local function yield()
os.queueEvent( "sleep" )
coroutine.yield( "sleep" )
end
This basically queues an event and pulls it directly by using corotine.yield( which is used by os.pullEvent ). I believe this takes 0.02 seconds or something.Posted 09 December 2014 - 08:55 PM
Well have you tried doing this?This basically queues an event and pulls it directly by using corotine.yield( which is used by os.pullEvent ). I believe this takes 0.02 seconds or something.local function yield() os.queueEvent( "sleep" ) coroutine.yield( "sleep" ) end
I believe that using any method of yielding in CC takes a minimum .05 seconds, which is the length of a minecraft tick. So this would take .05 seconds, which is the same as using sleep(0)
Posted 09 December 2014 - 09:40 PM
Have you even tried this? I'm pretty sure this is faster than sleepWell have you tried doing this?This basically queues an event and pulls it directly by using corotine.yield( which is used by os.pullEvent ). I believe this takes 0.02 seconds or something.local function yield() os.queueEvent( "sleep" ) coroutine.yield( "sleep" ) end
I believe that using any method of yielding in CC takes a minimum .05 seconds, which is the length of a minecraft tick. So this would take .05 seconds, which is the same as using sleep(0)
Proof that it's faster
local w, h = term.getSize()
local function yield()
os.queueEvent( "sleep" )
coroutine.yield( "sleep" )
end
local start = os.clock()
for x = 1, w do
for y = 1, h do
term.setCursorPos( x, y )
term.write( "0" )
end
yield()
end
local _end = os.clock()
term.clear() term.setCursorPos( 1, 1 )
print( _end - start ) -- Got the result 0,05 here
os.pullEvent( "key" )
start = os.clock()
for x = 1, w do
for y = 1, h do
term.setCursorPos( x, y )
term.write( "0" )
end
sleep( 0 )
end
_end = os.clock()
term.clear() term.setCursorPos( 1, 1 )
print( _end - start ) -- Got the result 2,25 here
Edited on 09 December 2014 - 08:55 PM
Posted 09 December 2014 - 09:59 PM
-snip
I did my own test, and found that you can run your yield function around 10,000 times in the amount of time it takes to do sleep(0) 100 times. This is honestly suprising to me. I was under the impression from other things that I had read that yielding required at least a mc tick, which it obviously does not.
I was unable to test it when I posted that. Was on a computer that refuses to run cc emulators, or mc.
edit:
edit2: Just noticed your original post was in reponse to dragon not the op.
Edited on 09 December 2014 - 09:16 PM