Jump to content




[1.4]turtle.select () is too slow


12 replies to this topic

#1 PonyKuu

  • Members
  • 215 posts

Posted 02 October 2012 - 09: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....

#2 Doyle3694

  • Members
  • 815 posts

Posted 02 October 2012 - 09:23 AM

turtle.select isn't that slow. I would imagine getting world block data takes longer.

#3 PonyKuu

  • Members
  • 215 posts

Posted 02 October 2012 - 09: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.

#4 Cruor

  • Members
  • 413 posts
  • LocationNorway

Posted 02 October 2012 - 09: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.

#5 immibis

    Lua God

  • Members
  • 1,033 posts
  • LocationWellington, New Zealand

Posted 02 October 2012 - 09:53 AM

View PostCruor, on 02 October 2012 - 09:46 AM, said:

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?)

#6 PonyKuu

  • Members
  • 215 posts

Posted 02 October 2012 - 09: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?

#7 PonyKuu

  • Members
  • 215 posts

Posted 02 October 2012 - 10:11 AM

View Postimmibis, on 02 October 2012 - 09:53 AM, said:

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

#8 Cruor

  • Members
  • 413 posts
  • LocationNorway

Posted 03 October 2012 - 07:18 AM

turtle.getItemCount, turtle.compare and turtle.select -seems- to take the same amount of time

#9 PonyKuu

  • Members
  • 215 posts

Posted 03 October 2012 - 09: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)...

#10 PonyKuu

  • Members
  • 215 posts

Posted 03 October 2012 - 08: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 :(/>'

#11 rewind

  • Members
  • 5 posts

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.

#12 Cloudy

    Ex-Developer

  • Members
  • 2,543 posts

Posted 25 November 2012 - 11:19 AM

Yes, and? Select is supposed to be slower.

#13 ChunLing

  • Members
  • 2,027 posts

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).





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users