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

os.pullEvent performance

Started by Thegameboy, 01 May 2015 - 10:39 PM
Thegameboy #1
Posted 02 May 2015 - 12:39 AM
This may be a silly question to ask, but how much CPU does os.pullEvent take up? I understand that it's really just a matter of coroutine.yield. However, are there other considerations, like the computer yielding excessively, taking up time? If there were 3 events in the event stack, key, rednet_message, and timer, and they all triggered a certain function, how much longer would this take to perform than if the functions were just executed right then and there? For example…


function keyPress()
  print("a key event was just triggered!")
end
function rednetMessage()
  print("a rednet message event was just triggered!")
end
function timer()
  print("a timer event was just triggered!")
end

while true do
  local event = os.pullEvent() -- 3 events initially in stack; will yield three times

  if event == "key" then keyPress()
  elseif event == "rednet_message" then rednetMessage()
  elseif event == "timer" then timer() end
end

as opposed to:


keyPress()
rednetMessage()
timer()

The reason I ask is because each time I have many timer events it seems to bog down the performance, even if there's very little execution they do when it's triggered.
InDieTasten #2
Posted 02 May 2015 - 12:47 AM
I guess it's less of the os.pullEvent, rather than your if statements. If you have many timers set up, you may be searching for them in the return values in a slow way.
Without being able to test code right now, you may just want to set up some benchmark code and test some scenarios
KingofGamesYami #3
Posted 02 May 2015 - 01:02 AM
What are you doing that could possibly involve that many timers? I usually only have about 3 or 4, max. Mostly I have a single timer running at any given time.

I'd guess there's a more efficient way to do whatever you are trying to do.
Bomb Bloke #4
Posted 02 May 2015 - 01:15 AM
When using "if" blocks with multiple "elseif"s, you'll get better performance if you put the conditions most likely to trigger up the top, eg:

if event == "timer" then timer() 
  elseif event == "key" then keyPress()
  elseif event == "rednet_message" then rednetMessage() end

If the majority of your events are timers, this prevents the second two conditions from needing to be checked for every other event that fires.

Potentially faster again is to take advantage of a table structure;

local myFuncs = {}

function myFuncs.key()
  print("a key event was just triggered!")
end
function myFuncs.rednet_message()
  print("a rednet message event was just triggered!")
end
function myFuncs.timer()
  print("a timer event was just triggered!")
end

while true do
  local event = os.pullEvent() -- 3 events initially in stack; will yield three times

  if myFuncs[event] then myFuncs[event]() end
end

… though this limits your ability to check other important elements, such as the timer IDs!