lua-users home
lua-l archive

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


Rici Lake wrote:
Here's a simple experiment: you can get string.gmatch to return
either an index (as an integer) or a capture (as a string). The
first option is slightly faster (but in my experience not enough
to make it useful if you're going to need the string eventually).

I should add that the simple benchmark I wrote does not control
for garbage collection costs. Running in a fresh Lua state as it
does, the garbage collector cycles fairly frequently, and finds
relatively few live objects.

I added 5 megabytes to the Lua memory space:
  local garbage = ('a'):rep(5e6)

and the time to execute the benchmark dropped from 5 seconds to
4.2 seconds. The additional memory used means that the garbage
collector runs less frequently, quite possibly enough so that
some strings are recycled in subsequent cycles.

On the other hand, adding 10,000 empty tables, which gives the
garbage collector more work to do in a collection cycle,
increased the time from 5 seconds to 6.7 seconds, slightly
more than double the time using only integer returns, but still
quite distant from 10 times as long.