lua-users home
lua-l archive

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


2011/4/13 Alexander Gladysh <agladysh@gmail.com>:
> Did you actually profile it?
>
> I bet that in plain Lua version with `string.format` is slower. (One
> extra global lookup, one extra table index, one extra function call.)

hmmm... weak argument, you can do:

local format = string.format

to get rid of the lookups but this is a standard Lua idiom.

Otherwise I don't have any benchmark and I'm not going to do it but I
note that string.format is a Lua native function that is supposed to
be optimized.

The expression

'[' .. s .. ']'

implies:
- the creation of temporary string bigger that s itself
- the copy of the original string to the new allocated memory
- append the additional data

all this repeated two times. In general this pattern is suboptimal
because of the repeated heap allocation and memory copying. Normally
string.format has some chance to follow a more optimal pattern.

Francesco