Lua speed testing

Pazyryk

Deity
Joined
Jun 13, 2008
Messages
3,584
If you're like me you don't look in the Tutorials section much. So this is just a plug for my Lua speed tests here. I'm comparing different blocks of code to try to understand how best to optimize my Lua coding. It has a practical side (sure) but it is partly just for fun.

Go look if you are dying to know whether ipairs is faster than pairs (it's actually slower) or other mostly nonessential coding arcanum. If you want to see something specific tested, make a suggestion here or in the tutorial thread.

I pasted below one of the tests (OK, this one is not really representative... it's the biggest speed difference I've seen in any of my comparisons):

-------------------------------------------------------
[Edit: I changed the test to add a counter]

Test 7: Traversing a Lua table vs. traversing a GameInfo table

Code 1:
Code:
	local n = 0
	for i = 1, 1000 do
		for row in GameInfo.Units("Cost < 200") do
			n = n + 1
		end
	end
	print(n)

Code 2:
Code:
	local tempTable = {}
	for row in GameInfo.Units() do
		tempTable[row.ID] = row
	end

	local highestID = #tempTable

	local n = 0
	for i = 1, 1000 do
		for j = 0, highestID do
			local row = tempTable[j]
			if row.Cost < 200 then
				n = n + 1
			end
		end
	end
	print(n)

Results:
Code 1: Code 1: 0.74800000000001 (6800%)
Code 2: 0.01099999999999 (100%)
--prints 57000 in both cases

Conclusion:
Traversing database tables is really slow compared to what Lua can do with a Lua table! If you need to iterate over a database table very much, move its contents over to a Lua table.
 
Top Bottom