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

[1.4]turtle.select () is too slow

Started by PonyKuu, 02 October 2012 - 08:17 AM
PonyKuu #1
Posted 02 October 2012 - 10:17 AM
Well the title is an issue, not a suggestion, but…

What I'm trying to make now - is a branch mining program. And I don't want my turtle to mine useless blocks, so I have a number of slots with "useless" blocks - such as smoothstone, dirt, gravel and so on, and let the turtle compare the block and each of useless blocks before mining. Here is my function:


function isUseful (side, range)
	for i=1,range do
		turtle.select(i)
		if tCompare[side]() then
			turtle.select (1)
			return false
		end
	end
	turtle.select (1)
	return true
end

tCompare is just a table containing all the compare function, so I can pass a side to the "isUseful" function, and not make all the "isUsefulUp", "isUsefulDown" stuff. "range", basically, means how many slots contain "useless" blocks.

So issue is time. If that block is not "useless", turtle hangs for a second or even more, while it checks all the "useless" slots (there are six of them)

There are two ways to fix this issue:
First one - is to make turtle.select() work faster. I'm not sure if it is possible.
Secont one - is to introduce a new function, which compares a block in the <slot> to a block to the <side> of turtle without selecting that slot.

And I'm sorry for my bad English since I'm not a native speaker….
Doyle3694 #2
Posted 02 October 2012 - 10:23 AM
turtle.select isn't that slow. I would imagine getting world block data takes longer.
PonyKuu #3
Posted 02 October 2012 - 10:28 AM
Actually, it IS slow. When my tunnel turtle is working, I can tell when it refuels by the freezes during the operation…
Maybe comparing adds a delay too, but I think the main slowdown is the turtle.select() function.
Cruor #4
Posted 02 October 2012 - 10:46 AM
Selecting slots takes 1 tick(.05s). operations like moving, turning, digging, placing takes .4 sec

if .8 sec delay hurts your program, you are doing something wrong/inefficient.
immibis #5
Posted 02 October 2012 - 10:53 AM
Selecting slots takes 1 tick(.05s). operations like moving, turning, digging, placing takes .4 sec

if .8 sec delay hurts your program, you are doing something wrong/inefficient.
When you are selecting and comparing 4 times, the selecting takes half as long as the mining, which is significant.
(Does compare also take 0.05s?)
PonyKuu #6
Posted 02 October 2012 - 10:53 AM
Well, I've posted the code here. I don't think there are the way to improve it… It makes a selection maximum 7 times for each function call (range == 6 for now)
And how much time do compare operations take?
PonyKuu #7
Posted 02 October 2012 - 11:11 AM
When you are selecting and comparing 4 times, the selecting takes half as long as the mining, which is significant.
(Does compare also take 0.05s?)
Yeah, and actually, when you make a 3 by 1 branch , there are 4 move-and-dig operations and 8 "isUseful" calls. When that block is a smoothstone - it's not a big deal, since you can place it in the first slot, and isUseful() returns false at the first iteration. But if it is not "useless" - it checks all 6 slots, and then selects 1'st slot, since I don't like when selection window has a random position.

Also, here is my refuel function:


local function refuel (amount)
    for i=1,16 do
	    if turtle.getItemCount (i) > 0 then
		    turtle.select (i)
		    while turtle.refuel (1) do
			    if turtle.getFuelLevel () >= amount then
				    return true
			    end
		    end
	    end
    end
    turtle.select (1)
    return false
end
I think, it is pretty efficient - it doesn't select any empty slots, and I think turtle.getItemCount() is very fast. It seems like all the functions, which are don't change the state of turtle (position, or selected slot) are performed very fast
Cruor #8
Posted 03 October 2012 - 08:18 AM
turtle.getItemCount, turtle.compare and turtle.select -seems- to take the same amount of time
PonyKuu #9
Posted 03 October 2012 - 10:41 AM
Did you test it? I don't have access to ComputerCraft right now… I think, some sort of loop should be nice… like this:


for i = 1, 1000 do
	local slot = math.fmod(i, 16) + 1
	turtle.select (slot)
end

One could run this and measure the time, required to complete the program, and then do the same thing for turtle.getItemCount(slot)…
PonyKuu #10
Posted 03 October 2012 - 09:35 PM
Tested that thing. With turtle.getitemCount() program finished almost instantly, and with turtle.select() it ran for some seconds or even some tens of seconds… Which means I shouldn't scrap my refuel function :(/>/>'
rewind #11
Posted 25 November 2012 - 09:24 AM
I can concur that select is far slower than other methods. Boo to all the people who posted otherwise. Do 100 selects vs itemcounts and see the difference.
Cloudy #12
Posted 25 November 2012 - 11:19 AM
Yes, and? Select is supposed to be slower.
ChunLing #13
Posted 26 November 2012 - 06:33 AM
If you're being really efficient about it you need to turn the turtle four times for each time you move, which takes more time than looping about to compare all the things. Basically, if the turtle is working more carefully then it has to work slower, that's entirely reasonable and intentional.

You're already making the process go as fast as reasonable by putting the most likely "useless" blocks first. All you need to do is add a detect so that the computer skips the compare loop if there's nothing there (though I guess you might be checking that before even calling this function).