This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
HDeffo's profile picture

Lua speeds

Started by HDeffo, 14 April 2015 - 10:16 AM
HDeffo #1
Posted 14 April 2015 - 12:16 PM
I am currently working on a program that is speed crucial so I am attempting to write it in the best format to squeeze everything I can out of it. Has anyone ever tested and made a really comprehensive list of various speeds for logic in lua. Things like which is faster repeat or while, how much of a difference does each logic check make in an if statement, speeds of different java defined functions (such as fs, getmetatable, etc), what effect each math operator has on speed, string concatting versus table.concat, difference in look up speed from local to globals, etc.


I will be trying to test everything I can in ComputerCraft but I figured if anyone has already done any of these it may be nice to just share results instead of doing the tests all over again.
Lupus590 #2
Posted 14 April 2015 - 12:44 PM
I can't confirm this but I heard that you should use local variables/functions as it's faster to access them.

There is a performance tester on the forums somewhere (I will link if I find it)

Edit: can't find it, but a basic way would be to print os.time before and after the rest of the program
the difference between the times is how long it took to run the program.

a few more tips, print directly to the terminal, and override the top level coroutine

you will also want your 'high speed' program to be the only one in the lua vm, this means only having one computer, and that computer is only running your 'high speed' program
Edited on 14 April 2015 - 11:26 AM
ElvishJerricco #3
Posted 14 April 2015 - 01:33 PM
You're not going to get much out of scraping the ifs and whiles of your program. It's going to come down to architecture. How efficient can you make the general idea, rather than individual chunks of code. Don't look for optimization where there is none. For example, while and repeat have the same performance. And a for loop is slower, but only because it's writing the "i = i + 1" line and the condition check for you, and the difference is negligible enough that worrying about it is dumb.

Point is, there's no use trying to shave instructions in a chunk of code. At most, you'll save a few. The real performance cost comes with how big your data is and the big O efficiency of your method of processing it.
H4X0RZ #4
Posted 14 April 2015 - 01:36 PM
If you want it to work only for you (or it is meant for a modpack which you "control") you can install CC Tweaks.
Just enable the LuaJC compiler, and it should work a lot faster.
SquidDev #5
Posted 14 April 2015 - 04:09 PM
Just a few other micro-optimisations:

Cache global access and use String metamethods:

string.sub(a, 1, 1) -- slower than
a:sub(1, 1) -- slower than
local sub = string.sub
...
sub(a, 1, 1)

Same applies for tables:

table.insert(a, true) -- slower than
a[#a + 1] = true

Gotta agree with ElvishJerricco, you should work on making the algorithms you use as fast as possible. There is a famous quote which is pulled out every time someone asks about optimisation, to warn people off it.
Premature optimization is the root of all evil

However, the full quote is as follows:
We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%.
Profile your code - write stubs for CC only methods and then use LuaProfiler to check how well it runs, optimise them, and repeat. Don't optimise for the sake of readability, and don't optimise a method because it might be slow, you haven't checked.
Edited on 14 April 2015 - 02:48 PM
ElvishJerricco #6
Posted 14 April 2015 - 04:25 PM
- snip -

Well said.

Also, if you really wanna go deep on performance, you can use LASM to get the best efficiency possible out of bytecode =P (this is a terrible idea)
H4X0RZ #7
Posted 14 April 2015 - 04:49 PM
- snip -

Well said.

Also, if you really wanna go deep on performance, you can use LASM to get the best efficiency possible out of bytecode =P (this is a terrible idea)
//OFFTOPIC
As I don't want to necro your LASM thread, I'm going to ask here: How does LASM make the production of other languages simpler?
HDeffo #8
Posted 14 April 2015 - 04:51 PM
I should probably have specified the reason I am micro optimizing this program. This is a new language designed for redstone written in lua. However while it is running plenty fast currently with the way I have it scripted now I wanted to make it run as close to the same speeds you could expect from running vanilla lua. This is mostly my last minute squeeze the last of the lemonade out attempt :P/>/>


And I am actually also curious how LASM makes writing new languages easier since after all this whole thread applies to a new language I've been working on
Edited on 14 April 2015 - 02:53 PM
SquidDev #9
Posted 14 April 2015 - 04:55 PM
Nicely made! But there is no real point of this. *Is gonna try make a language too*

As I said above, the best reason for it is that it's easier to target an assembly language when building a compiler for a language than bytecode, so if you make a language, you'll have a better time compiling down to LASM than to bytecode or Lua.

LASM keeps track of constants and is easier to write then mucking about with LuaAssemblyTools or the ASM framework, though you may want to have a look at the latter too.
ElvishJerricco #10
Posted 14 April 2015 - 05:46 PM
Yea the asm.lua framework could be better. We need to add the ability to create inner functions and a few other things.