lua-users home
lua-l archive

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


> I have a function I would like to optimize (yes, I've done profiling).
> it transforms string like "3-7" into "3,4,5,6,7".
> 
> After trying out a couple of variants I still couldn't gain much.
> May be someone on the list could do better.
> 
> Here are my versions:
[snip]
> 
> Both runs on the same speed and profiling tells that most time spent in
> a [for] loop.

Yes, a challenge, I love that!

Hum, I don't know if your profiling or mine are wrong, but on my system (PII
300, Win98), the first routine 
is awful, 10 time slower than the second one.
I made a little change, and I go a little faster... with a small change:

function numeric_range_tranformer3(s)
	ctor = function(from, to)
		local n1 = tonumber(from)
		local n2 = tonumber(to)
		if n1 >= n2 then return from end
--		local s = from
		local t = {from}
		for i = n1+1, n2 do
			t[i - n1 + 1] = i
		end
		return table.concat(t, ",")
	end
	return string.gsub(s, "(%d+)-(%d+)", ctor)
end

or even better, make:
		local t = {}
		for i = n1, n2 do
			t[i] = i
		end
		return table.concat(t, ",", n1, n2)

a little faster, doing less computing...

Tell me if it is better, using your profiling.

Regards.

-- 
--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--
Philippe Lhoste (Paris -- France)
Professional programmer and amateur artist
http://jove.prohosting.com/~philho/
--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--

+++ GMX - Mail, Messaging & more  http://www.gmx.net +++
Bitte lächeln! Fotogalerie online mit GMX ohne eigene Homepage!