Jump to content




Optimization and micro-optimization


43 replies to this topic

#41 The Crazy Phoenix

  • Members
  • 136 posts
  • LocationProbably within 2 metres of my laptop.

Posted 12 July 2016 - 07:42 PM

View PostBomb Bloke, on 12 July 2016 - 12:31 AM, said:

It can be done, but I can't think of any method that'd actually be faster than simply sticking the values in one index at a time.

For example, an array the size you're talking about can be built within a twentieth of a second via a simple "for" loop - you'd need to go a lot larger than that before it'd start to matter.

Timed it and it took 0.15 seconds, 15x more than you said. Initializing the array with the correct size initially could reduce that time by quite a bit.

#42 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 12 July 2016 - 08:00 PM

That's only 3 times more than 0.05. And it varies depending on the computer - it even varies depending on the test. Also, tables are heavily optimized from the Java side of LuaJ.
I performed a simple test, the script test being
local t = {}
local time = os.clock()
for i = 1, 1048576 do
t[ i ] = i
end
print( os.clock() - time )
And I got these (rather varied) results:
Posted Image

Edited by KingofGamesYami, 12 July 2016 - 09:09 PM.


#43 The Crazy Phoenix

  • Members
  • 136 posts
  • LocationProbably within 2 metres of my laptop.

Posted 13 July 2016 - 09:37 AM

I was also creating 1,048,576 zeros in my test, so that may have something to do with it. I got similar results every time I ran it (about 0.15 seconds).

Edited by CrazyPyroEagle, 13 July 2016 - 09:38 AM.


#44 HDeffo

  • Members
  • 214 posts

Posted 19 July 2016 - 08:34 AM

View PostThe Crazy Phoenix, on 11 July 2016 - 10:58 PM, said:

If I'm trying to initialize a table of dynamic or insanely high size, how can I optimize it such that LuaJ will initialize its array with the size I want? For example, a 1,048,576-sized array.

Whilst you address many interesting optimizations, to some, there really is no way of abusing them when using dynamic sizes.

please note the following is all untested. I remember reading on the lua talk pages of a little "hack" where you could initialize an empty table then use some bytecode hacking to set it to a predefined size of nil values then save it again as the new larger table. That being said as it is untested I am not sure the proper bytecode to achieve this nor am I sure how this would affect run times. Unless you feel like delving into the really complicated and needless parts of Lua you are best off sticking with bomb bloke's suggestion. In LuaJ a for loop doesn't increase run times at all not even to declare its running variable or at least short enough time that a profiler couldn't pick it up. Alternatively as stated declaring it full of nil values prior to running that for loop would still be faster but no one wants to type out tbl = {(1 million+ nils)}. For loops seem the most practical solution to this in any case





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users