Search code examples
c#lualuainterface

LuaInterface C#: dostring vs dofile. Is it worth loading a script that will be called numerous times into memory?


Can anyone tell me if I can reasonably expect any performance gain by loading a lua script that will be called repetitively into memory for execution through LuaInterface's dostring() functionality rather than dofile()?

Am I correct in assuming this will perform better by reducing file-system access each iteration?

Is there some way to cache the script within the Lua VM?


Solution

  • If you want to execute some lua code for many times, the better way is using LoadFile OR LoadString. Load lua code as a LuaFunction, like:

    LuaFunction lf = xxx.LoadString("some lua code");   // xxx is an instance of LuaInterface
    lf.Call();   // you can also deliver some arguments
    

    This is much faster than DoFile AND DoString. Because it needs just one time compiling.