lua-users home
lua-l archive

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


Am Dienstag, 23. Februar 2010 11:01:45 schrieb steve donovan:
> 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.

I did it, here are the timings (i removed "sys" timing). Only one core was 
used of a Q9550 @ 2.83GHz, lua 5.1.4


tk@gentoo-c2q ~ $ time lua -e "a={};for i=1,20000000 do a[#a+1]=i end"
real    0m6.863s
user    0m6.579s


tk@gentoo-c2q ~ $ time lua -e "a={};for i=1,20000000 do table.insert(a,i) end"
real    0m7.768s
user    0m7.466s


tk@gentoo-c2q ~ $ time lua -e "a={};for i=1,20000000 do a[i]=i end"
real    0m1.322s
user    0m1.050s



The same with local:

tk@gentoo-c2q ~ $ time lua -e "local a={};for i=1,20000000 do a[#a+1]=i end"
real    0m5.719s
user    0m5.427s

tk@gentoo-c2q ~ $ time lua -e "local a={};for i=1,20000000 do 
table.insert(a,i) end"
real    0m7.225s
user    0m6.941s

tk@gentoo-c2q ~ $ time lua -e "local a={};for i=1,20000000 do a[i]=i end"
real    0m0.966s
user    0m0.697s



Bye,

    Torsten