[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: string buffers using string.gsub faster than table.concat
- From: David Manura <dm.lua@...>
- Date: Mon, 27 Aug 2012 21:33:35 -0400
On Sun, Aug 26, 2012 at 1:29 AM, Javier Guerra Giraldez
<javier@guerrag.com> wrote:
> just replacing each inner 'i' by 'j', and using it for the table index
> instead of #ts+1, i got:
The outer loop i's (provided merely for benchmarking) are not
referenced anywhere, but you're right about the #ts+1 overhead being
significant. Here's an updated benchmark:
-- test.lua
local mode = arg[1] or 'A'
local N = arg[2] or 1000000
local M = arg[3] or 10
local result
if mode == 'A' then
for _=1,M do
result = ((' '):rep(N):gsub('().', function(i) return
string.char(i % 256) end))
end
elseif mode == 'B' then
for _=1,M do
local ts = {}
for i=1,N do ts[i] = string.char(i % 256) end
result = table.concat(ts)
end
end
Results: (xeon/cygwin/make posix)
A: 5.3 5.4 5.3 (lua 5.2.1)
A: 5.1 5.1 5.1 (lua 5.1.5)
B: 4.6 4.6 4.6 (lua 5.2.1)
B: 4.5 4.4 4.4 (lua 5.1.5)
By further moving the `(' '):rep(N)` out of the loop, we get only a
small improvement:
A: 5.2 5.1 5.3 (lua 5.2.1)
A: 5.0 4.9 5.0 (lua 5.1.5)
B: 4.5 4.6 4.6 (lua 5.2.1)
B: 4.4 4.4 4.4 (lua 5.1.5)
So, the string.gsub approach is not faster, but it's close. One thing
can be said: it has a more functional style.
This also reminds me that string regular expression libraries can be
abused in other ways, such as to solve Boolean satisfiability problems
(SAT) [1-2], but Lua's pattern matching library lacks support for
alternation on backreferences.
[1] http://nikic.github.com/2012/06/15/The-true-power-of-regular-expressions.html
[2] http://perl.plover.com/NPC/NPC-3SAT.html