[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: the most optimal string "trim" implementation
- From: David Manura <dm.lua@...>
- Date: Sat, 26 Dec 2009 23:16:28 -0500
On Sat, Dec 26, 2009 at 6:31 PM, Shmuel Zeigerman wrote:
> [this] slightly outperformed your variant, except for the all-space strings,
> where it performed worse (which I don't understand why).
> function trim(s)
> local from = s:find("%S")
> return from and s:match(".*%S", from) or ""
> end
This variant of yours performs much better in the worst case and has
about the same performance overall as my last one:
function trim(s)
local from = s:match"^%s*()"
return from > #s and "" or s:match(".*%S", from)
end
For some reason, s:find"^%s*$" is about twice as fast as s:find"%S"
given s = (" "):rep(100).