loadstring

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

loadstring

Post by Ref »

Anybody know what is going on behind the scene with loadstring?
Was working with threads and trying to get a faster way to convert a string to a table.
My first approcah was:

Code: Select all

function extractPoints( stringData )	 -- string =>  point table{x,y,z}
	local temp = stringData:split() -- p1x,p1y,p1z,... => temp{p1x,p1y,p1z, ... }
	local reconstructedTable = {}
	local field	= 1
	for i = 1, #temp, 3 do		-- temp{p1x,p1y,p1z, ... } => table{{p1x,p1y,p1z},{p2x,p2y,p2z}, ... }
		reconstructedTable[ field ] = { temp[i], temp[i+1], temp[i+2] }
		field = field + 1
		end
	return reconstructedTable
	end

function string:split( sep ) -- parces string 'x,y,z' into x y z - default separator == comma
		local sep, fields	= sep or ",", {}
		local pattern	= string.format( "([^%s]+)", sep )
		self:gsub(pattern, function(c) fields[#fields+1] = c end)
		return fields
		end
That seemed just too complex to be efficient so I tried:

Code: Select all

function string2table( string )
	assert( loadstring( string ))()
	end
Both worked (slightly different string formating) but the loadstring approach was ~20% slower than my first approach.
Any ideas why?
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 56 guests