LuaInterface C #: dostring vs dofile. Should I load a script that will be called multiple times in memory?

Can someone tell me if I can reasonably expect any performance improvement by loading a lua script that will be repeated in memory to be executed via LuaInterface dostring() , not dofile()

Am I right in thinking that it would be better if you reduce access to the file system at each iteration?

Is there a way to cache a script in a Lua VM ?

+4
source share
2 answers

If you want to execute any lua code repeatedly, the best way is to use LoadFile OR LoadString. Download the lua code as a LuaFunction, for example:

 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 he only needs one time compilation.

+1
source

Am I right in thinking that it would be better if you reduce access to the file system at each iteration?

In general, you should rather be guided than to assume, but it is pretty cut and dried: "disk IO, compile, execute" vs "execute". Of course, the latter will be faster.

Is there a way to cache the script inside the Lua virtual machine?

If you dofile or dostring any global variables that the script you created will be displayed, will be read / callable. If you want your scripts to not change the global virtual machine namespace, you can open the API routine (in the host) that your script can call to register the callback.

+1
source

All Articles