edited 1×, last 04.06.11 06:57:02 am
Forum
CS2D Scripts Trying to rotate a 2D arrayTrying to rotate a 2D array
6 replies 1
archmage has written
1
2
3
4
5
2
3
4
5
for i = 1, s do 			for j = 1, s do 				t[i][j] = t[j][i] 			end 		end
doesnt work cuz u'll flip stuff twice i.e. the for loop expands to
t[1][1] = t[1][1]
t[1][2] = t[2][1]
t[2][1] = t[1][2] << look at line above
t[2][2] = t[2][2]
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
function rot_ccw(t) 	local tt = {} 	for a, b in pairs(t) do 		for c, _ in pairs(b) do 			--assuming rectangular array. if not, replace #b with the max width of array. 			if not tt[#b - c + 1] then tt[#b - c + 1] = {} 			tt[#b - c + 1][a] = t[a][c] 		end 	end 	return tt end
btw dont see the purpose of 's'
there are only 3 possible new orientations; might as well write a function for each.
which would save more cpu cycles?
1
2
3
2
3
local n = #b if not tt[n - c + 1] then tt[n - c + 1] = {} tt[n - c + 1][a] = t[a][c]
or
1
2
2
if not tt[#b - c + 1] then tt[#b - c + 1] = {} tt[#b - c + 1][a] = t[a][c]
or is lua smart enough to make the two equivalent? or does lua reevaluate #b every time?
I do not actually need to rotate it I just need to index it as if it where rotated. So this is better for me:
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
function getRotVal(t, x, y, r) 	r = r % 4 	if ( r == 0 ) then 		return t[x][y] 	elseif ( r == 1 ) then 		return t[#t-x+1][y] 	elseif ( r == 2 ) then 		return t[#t-y+1][#t-x+1] 	elseif ( r == 3 ) then 		return t[x][#t-y+1] 	end end
http://www.lua.org/source/5.1/lobject.h.html#sizearray
First, getting n requires 1 push from the local onto the stack and one pointer dereference when its ultimately needed. However, each call to #b, assuming that Lua is smart enough to not check the __len method if its not overloaded, requires one push from the local, a pointer dereference, a pop from the stack, and another push from the stack. Lua won't inline the optimization so once the table is indexed, the number will be consumed and popped off of the stack and this will be repeated.
However, in most cases, the following will happen:
1. If the pointer offset between the local stack, b, and n are relatively large, and you have a few lines of code executing in between each table index, then chances are each of index instruction will cause a cache miss with a CPU cycle penalty >> theoretical cost of the instruction. (This depends on the CPU though so can't be certain, in most cases the address of the L state, local, global will be on the primary cache and a few of the more frequently used objects will be on the secondary cache, everything else is up for grabs and depends on how well Lua manages its memory)
2. If not, then the difference will still most likely be negligible.
In other words, these two have essentially the same runtime, everything else being accounted for. People don't usually worry about cycle costs when they use Lua or some other type of VMed languages.
Lee has written
People don't usually worry about cycle costs when they use Lua or some other type of VMed languages.
ya i think i'm paranoid worrying about stuff in the nanosecond range
1