lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi,

First of all, I really appreciate the quick and meaningful answers to the
unpack() question, thank you!

Second, since we use table inserts a lot in our projects I did some tests on
how the speed of it compares as mentioned by Aaron and Javier. I thought it
would be nice to share the results.

The code for the tests:

function Test1(a)
	for i = 1, 5000 do
		table.insert(a, i)
	end
end

function Test2(a)
	for i = 1, 5000 do
		a[#a + 1] = i
	end
end

function Test3(a)
	for i = 1, 5000 do
		a[i] = i
	end
end

The test was done on an old AMD 900 running Windows XP, the time checked by
QueryPerformanceTimer, running on Lua 5.1 with LuaJIT. The results vary a
bit for several reasons, but still seem clear:

Test 1: 0.0054836575873196
Test 2: 0.0047802170738578
Test 3: 0.0033275177702308

Test 1: 0.0062007885426283
Test 2: 0.004486883059144
Test 3: 0.0032674539834261

Test 1: 0.0065896646119654
Test 2: 0.0048497775569558
Test 3: 0.0028129266574979

Test 1: 0.0052182609215379
Test 2: 0.0065751373767853
Test 3: 0.0039544124156237

Test 1: 0.0055099176242948
Test 2: 0.0050414223223925
Test 3: 0.0026796702295542

Test 1: 0.0059292451478541
Test 2: 0.0052207754924893
Test 3: 0.0027126353234053

Test 1: 0.006284317933023
Test 2: 0.0047106547281146
Test 3: 0.003342604264617

Test 1: 0.0054825400002301
Test 2: 0.0043044574558735
Test 3: 0.0037068957462907

So it seems in some cases using a local counter (Test3) in stead of
table.insert (Test1) can almost half the execution time, and, the difference
between a[#a + 1] and using a local counter also seems significant.

	Thanks again,

			Hugo


-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br]On Behalf Of Aaron Brown
Sent: vrijdag 4 augustus 2006 22:14
To: Lua list
Subject: Re: Using unpack() twice in a single function call


Javier wrote:

> it might be interesting to compare the speed of
> t[#t+1]=exp versus table.insert (t, exp)

> i guess it should be the same,

I think most of the difference in speed comes from the
overhead of the function call.

--
Aaron