This topic contains 4 replies, has 1 voice, and was last updated by Eliot Muir 5 years, 1 month ago.
Is table.insert(T, 1) or T[#T+1] = 1 faster
You must be logged in to reply to this topic.
This topic contains 4 replies, has 1 voice, and was last updated by Eliot Muir 5 years, 1 month ago.
When I am appending data to a Lua table an idiom I use a lot is:
T[#T+1] = 1
I noticed a lot of people use the Lua table insert function which is like:
table.insert(T,1)
I tend to prefer the former expression since I am used to it. I was curious if there was any performance benefit one way or the other.
I wrote some test code and found that table.insert took twice as much time in the editor. This is because of the fact it creates annotations which has a little bit of overhead. Running the channel in production without the overhead of annotations and the table.insert method seems to be marginally slower than T[#T+1] = 1.
Screen shot of the logs.
And the annotations.
I dropped the channel into a github repo if anyone is vaguely interested 🙂 :
https://github.com/interfaceware/benchmark/blob/master/Time_Test-From-W7q9a6r73UJNnL/main.lua
Did another test comparing ipairs vs. a for loop.
https://github.com/interfaceware/benchmark/blob/master/Speed_of_ipairs-From-eYMAZvjkoaAdKK/main.lua
ipairs is slower in the editor due to annotations but faster than the simple for loop at runtime.
You must be logged in to reply to this topic.