lua-users home
lua-l archive

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


I've noticed a tremendous performance cost for
executing code that looks like the function reproduced
below. All the complicated code has already been
executed by the time I get here, and yet 90% of the
program's time is spent in this function. I'm assuming
it is because of the tables that are being created,
possibly a couple of thousands of them. Almost all of
the elements use consecutive integer indices. Is there
some way I can preallocate the tables that it is
generating, or reuse them from one invocation to
another?

Sorry for the ugly formatting. I hope it is still
readable.

Thanks.
-----------------------------

function
createFieldDataEntriesForOneDataGenerator(dataGenerator)

    local data = dataGenerator()
    
	local subTable = {}
	local subTableBlock = {}
	local subTableType
	
    for i, element in ipairs(data) do
        if data[i][1] == "<type>" then
			subTableType = data[i][2]
        elseif data[i][1] == "</type>" then
        elseif data[i][1] == "<block>" then
            subTableBlock =
{Number=data[i][2],Dup=data[i][3]}
        elseif data[i][1] == "</block>" then
			subTable[#subTable+1] = subTableBlock
        else 
            if trim(data[i][2]) ~= "" then
                subTableBlock[#subTableBlock+1] =
{data[i][1],data[i][2]}
            end
        end
    end
    
    return subTable, subTableType
    
end