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

How can I make this program faster?

Started by CCJJSax, 27 May 2016 - 04:45 PM
CCJJSax #1
Posted 27 May 2016 - 06:45 PM
This program has no sleep() in it, yet it's not that fast. It's run on a command computer and it's designed to find all attached blocks to the block underneath the computer and replace them with air.

I know that these computers have the ability to erase the blocks pretty fast, but this is rather slow.

http://pastebin.com/ZpUjb3uP
Lupus590 #2
Posted 27 May 2016 - 06:54 PM
Have you tried looking at this? http://www.computercraft.info/forums2/index.php?/topic/24574-optimization-and-micro-optimization/

You may also want to try using http://computercraft.info/wiki/Commands.execAsync
KingofGamesYami #3
Posted 27 May 2016 - 07:45 PM
commands.getBlockInfo is slow. Try running a few in parallel.
valithor #4
Posted 27 May 2016 - 07:48 PM
commands.getBlockInfo is slow. Try running a few in parallel.

To expand this thread will likely help a lot if you get stuck: http://www.computercraft.info/forums2/index.php?/topic/24627-getblockinfo-async/

execAsync will not work due to the fact getBlockInfo can not be used in async.
CCJJSax #5
Posted 27 May 2016 - 11:37 PM
I must not know how to do parallel functions. I looked at the wiki and now it freezes when I put it in. I tried to add a return to the function it calls, but that didn't help and errored saying it was looking for a function and got boolean when I had it return true.
KingofGamesYami #6
Posted 27 May 2016 - 11:45 PM
A common mistake is to call the function and pass the result to the parallel function. Here's an example of how I would approach this task:


local tCoords = {}
--#add a bunch of coordinates to the tCoords table
local function execCoords()
  while #tCoords > 0 do
    local x, y, z = (unpack or table.unpack)(table.remove( tCoords, 1 ))
    local info = commands.getBlockInfo( x, y, z )
    --#do stuff with the info
  end
end
local tExecute = {}
for i = 1, 10 do
  tExecute[ i ] = execCoords
end
parallel.waitForAll( unpack( tExecute ) )