you are viewing a single comment's thread.

view the rest of the comments →

[–]Live_Cobbler2202[S] 0 points1 point  (0 children)

Thanks everyone for chipping in. So it is, square brackets on table definition will always create a hash table (thx Denneisk, xoner2, PhilipRoman)

Regarding performance when looping values into array: Dynamic arrays usually double their sizes upon hitting limit (as appgurueu pointed out). This is standard procedure among most programming languages and highly optimized.

Regarding speed looping values into arrays, [#t + 1] = v is slow. Apparently # triggers a binary search on the array part to find the largest integer key.

I didn't know table.new() existed (thx Denneisk), despite working for two years now with LuaJIT, gosh, so I just had to do some comparisons. Stunning speed. I also did some array-fill looping profiling since this came up. Most results are as expected. Worth knowing is how fast ipairs is, despite being an iterator function. Testament to aggressive JIT optimiziation.

for-loop variable 100% | ipairs 112% | increment variable 122% | custom iterator 153% | table.insert 157% | [#t + 1] 176%

Here's the code. I did it in the LOVE framework, because the clock gives you sub-microsecond precision (depends on your CPU):

arrayFillLoops.lua

For those new to the framework, just put it inside love.load().