• Lua indexing performance
    2 replies, posted
I made a testing program today for helping me chose between storing data methods in Lua. I really want to make my library more compact, so I decided to remove N lines with different local variable names. Ok I thought, great the code can be red better I said, but the data has to be stored somewhere else. Here is a bright Idea ... a local table containing all those with hashing the name and retrieving the value, however everything impacts the performance. Thought this article can be stored so others can see the Lua performance experiment I made. [code] local I = 0 local S = {} local A = {[1] = "track_",["x"] = "track_"} B = {[1] = "track_",["x"] = "track_"} local x = "track_" y = "track_" function Foo1(Arg) local Typ = type(Arg) if(Typ ~= "string") then print("bummer") return end return A[Arg] end function Foo2(Arg) return A[Arg] end function Foo3(Arg) return B[Arg] end function Foo4(Arg) return x end function Foo5(Arg) return y end local function Test(F,A,Name) print("Testing: "..Name) local s = os.clock() for j = 1,100 do for i = 1,14000000 do local x = F(A).."pieces" end end I = I + 1 S[I] = {((os.clock()-s)/100),Name} end function PresentScore() local T = Sort(S,{1,2}) for i = 1,I do local k = T[i].Key print(i..": "..S[k][1].." >> "..S[k][2]) end end Test(Foo1,"x","Key type handling") Test(Foo2,"x","Local table Hash indexing") Test(Foo2,1,"Local table Number indexing") Test(Foo3,"x","Global table Hash indexing") Test(Foo3,1,"Global table Number indexing") Test(Foo4,1,"Local variable") Test(Foo5,1,"Global variable") PresentScore() [/code] 1: 1.36901 >> Local variable 2: 1.53177 >> Global variable 3: 1.53719 >> Local table Hash indexing 4: 1.61159 >> Local table Number indexing 5: 1.63600 >> Global table Hash indexing 6: 1.77707 >> Global table Number indexing 7: 2.74061 >> Key type handling That shows how fast exactly the statement " t = type(A) " really is ( Comparing "Key type handling" and "Local table Hash indexing" time drops by a half) ! Indexing a global variable needs the same time as a hashing a local table, as well as global table hashing and local table number indexing. So hash is in fact faster than the number indexing. So tell me guys what do you think about that.
In stock Lua, type is a really fast function. It's been changed to support the extra types that Garry's Mod adds (Vectors, Angles, ...) and the speed was sadly lost.
[QUOTE=Willox;47484163]In stock Lua, type is a really fast function. It's been changed to support the extra types that Garry's Mod adds (Vectors, Angles, ...) and the speed was sadly lost.[/QUOTE] Hmm, that kinda sux ... local table hash indexing is so far my best option. When I remove the concatenation ( as it is very expensive ) I've got the following stuff: 1: 0.53929 >> Local variable 2: 0.67929 >> Local table Hash indexing 3: 0.69020 >> Global variable 4: 0.78166 >> Local table Number indexing 5: 0.83368 >> Global table Hash indexing 6: 0.97297 >> Global table Number indexing
Sorry, you need to Log In to post a reply to this thread.