lua-users home
lua-l archive

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


On Thursday, June 21, 2012 04:19:43 AM steve donovan wrote:
> And the point would be, that you can get Lua to do just about anything
> you like, at some performance cost. The sugar s[i] for accessing the
> 'character' at the ith index now slows down all string method lookups.
> 

The speed penalty isn't as great if you do:

    local string = require('string')
    getmetatable("").__index = function(s, i)
        return string[i] or string.sub(s, i, i)
    end

Michal's version was 2.25 times slower, this is only 1.15. But nothing is as 
fast as putting string.sub in a local variable. I don't like to use the string 
method sugar for this reason.

But as this thread has shown, if speed is important, you should be using 
LuaJIT anyway. Although then the local trick is sub-optimal to just writing 
string.sub everywhere. Which is why I wish there was a syntax for aliasing a 
variable.

-----
-- tom
telliamed@whoopdedo.org