lua-users home
lua-l archive

Re: array current size

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


On 2010年2月23日 12:01:45 +0200
steve donovan <steve.j.donovan@gmail.com> wrote:
> On Tue, Feb 23, 2010 at 11:57 AM, spir <denis.spir@free.fr> wrote:
> > instead of "table.insert(array,item)" or "array[#array+1] = item".
> 
> Those are often used; AFAIK the second idiom is considered pretty quick.
> 
> When in doubt, do a microbenchmark!
> 
> steve d.
=== results (rather stable):
using cached index: 5
using #array: 15
using insert: 22
======== code =============
time = os.time
N = 33333333
local t0 = time()
local array = {}
i = 0
for n=1,N do
	array[i] = n
end
local t = time() - t0
print("using cached index: "..t)
local t0 = time()
local array = {}
for n=1,N do
	array[#array+1] = n
end
local t = time() - t0
print("using #array: "..t)
local t0 = time()
local array = {}
for n=1,N do
	table.insert(array, n)
end
local t = time() - t0
print("using insert: "..t)
________________________________
la vita e estrany
http://spir.wikidot.com/

AltStyle によって変換されたページ (->オリジナル) /