lua-users home
lua-l archive

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


> > I try this test in
> > 
> > [...]
> >
> > And in each case concatenation approach is faster.
> > 
> > [...]
> >
> > Does anyone knows abount this?
> 
> Have you both agreed about how to test each solution? (I haven't seen
> that on the messages...) On my machine, the concat solution is
> faster than gsub for small arrays, but gsub gets better as the size
> grows. This is expected, as the concat solution is ~O(n^2), see
> http://www.lua.org/notes/ltn009.html.
> 
> With Lua w4, you can try the new "concat" function:
> 
> function bytestostring2 (bytes)
> local w = {}
> for i=1,getn(bytes) do w[i] = strchar(bytes[i]) end
> return concat(w)
> end
> 
> -- Roberto

Argh, I was about to say the same thing! :-)
Well, I can as well publish my results on Win32. Since the figures were too
small for clock(), I just finished my Win32 library (with a lot of dummy
functions!) to a pair of functions using the Pentium performance counter to get a
lot more precision. That's why I lost so much time :-)

Reusing the given functions:

function bytestostring(bytes)
  s = ""
  for i = 1, getn(bytes) do
    s = s .. strchar(bytes[i]) -- '..' create new string. Expensive!!
  end
  return s
end

function bytestostring40(bytes)
  local buf = strrep(' ', getn(bytes))
  local index = {n = 0}
  local rep = function (c)
    %index.n = %index.n + 1
    return strchar(%bytes[%index.n])
  end
  return gsub(buf, '(.)', rep)
end

function bytestostring41(bytes)
  local buf = strrep(' ', getn(bytes))
  local i = 0
  return (gsub (buf, '(.)',
    function(c)
      i = i + 1
      return strchar(bytes[i])
    end))
end

function FastBytesToString(bytes)
  s = {}
  for i = 1, getn(bytes) do
    s[i] = strchar(bytes[i])
  end
  return concat(s)
end

str = {}
for i = 32, 127 do tinsert(str, i) end
StartFineClock() -- return current raw counter value
print(slowbytestostring(str))
-- return time in seconds, in microseconds, in raw counter value
-- if given false argument, don't reset the counter
_, t = GetFineClockTime()
print(t)
print(bytestostring40(str))
_, t = GetFineClockTime()
print(t)
print(bytestostring41(str))
_, t = GetFineClockTime()
print(t)
print(FastBytesToString(str))
_, t = GetFineClockTime()
print(t)

lua -c -f "TestConcat.lua"

!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
bytestostring: 342.7809001476724

!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~	96
bytestostring40: 378.8189898942492

!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
bytestostring41: 313.4475712841796

!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
FastBytesToString: 232.1523455767855

The figures vary a bit from one run to the other, but are consistent.

BTW, I wonder why bytestostring40 returns two values (because of gsub) and
not bytestostring41?
Oh, I can answer myself, it is because the use of the seemingly useless
parenthesis around the return value of bytestostring41. Actually, they are not
useless! With the new syntax, it reduces the number of returned arguments to
the first one.
I note this because it is a good way for me (and perhaps others) to better
memorize this feature...

Regards.

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

GMX - Die Kommunikationsplattform im Internet.
http://www.gmx.net