[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Iterating pairs of keys from a table
- From: Tim Hill <drtimhill@...>
- Date: Tue, 28 Feb 2017 17:00:32 -0800
> On Feb 28, 2017, at 3:50 PM, Chris Berardi <cberardi@fastmail.com> wrote:
>
>
> On Tue, Feb 28, 2017, at 06:18 PM, Tim Hill wrote:
>>
>
> What about creating a tmp array?
>
> local t = {};
> for i=1,10000 do
> t['k'..tostring(i)] = true
> end
>
> t1 = os.clock();
> -- create tmp array
> local tbl_,ix = {},1
> for k,v in pairs(t) do tbl_[ix] = k; ix = ix + 1 end
>
> local k1,k2,num
> for i=1,#tbl_ do
> for j=i+1,#tbl_ do
> k1,k2 = t[i],t[j]
> num = num + 1
> end
> end
> t2 = os.clock();
> print('elasped time for '..num..' => '..(t2-t1)..' sec')
>
> Result average over 5 passes: 2.48 seconds
>
I thought of that originally, but I didn’t like the memory overhead involved in creating the array every time the generator was used (particularly for large tables).
—Tim