views:

1141

answers:

6

I asked a question about Lua perfromance, and on of the responses asked:

Have you studied general tips for keeping Lua performance high? i.e. know table creation and rather reuse a table than create a new one, use of 'local print=print' and such to avoid global accesses.

This is a slightly different question from Lua Patterns,Tips and Tricks because I'd like answers that specifically impact performance and (if possible) an explanation of why performance is impacted.

One tip per answer would be ideal.

+3  A: 

Lua Optimization Tips

Firas Assaad
A: 

The Lua mailing list much more info on this than you're likely to get here.

A: 

Keep tables short, the larger the table the longer the search time. And in the same line iterating over numerically indexed tables (=arrays) is faster than key based tables (thus ipairs is faster than pairs)

Robert Gould
+1  A: 
  • Making the most used functions locals
  • Making good use of tables as HashSets
  • Lowering table creation by reutilization
  • Using luajit!
Kknd
+1  A: 

If your lua program is really too slow, use the Lua profiler and clean up expensive stuff or migrate to C. But if you're not sitting there waiting, your time is wasted.

The first law of optimization: Don't.

I'd love to see a problem where you have a choice between ipairs and pairs and can measure the effect of the difference.

The one easy piece of low-hanging fruit is to remember to use local variables within each module. It's general not worth doing stuff like

local strfind = string.find

unless you can find a measurement telling you otherwise.

Norman Ramsey
+1  A: 

http://trac.caspring.org/wiki/LuaPerformance

Tobi