Help with your scripting

Optimisation Trick

Optimisation Trick

Postby Shando » 18 Jun 2015, 05:58

Hi All,

I was just writing some code that used a lot of math functions and came across this neat trick ( which I had forgotten :oops: ):

Code: Select all
local log10 = math.log10;
local floor = math.floor;

local iCount = floor ( log10 ( iIn ) / log10 ( 2 ) );


If you make the math functions (or any other functions I believe?) LOCAL, then they get accessed quicker :mrgreen:

Useful if you need to shave off a few microseconds here and there in your code (especially for sine and cosine - though I believe that lookup tables would, potentially, be faster still ;) ).

Regards

Shando
Alienware 17 (i7-4700MQ 2.4GHz 16GB GTX770M Win 10 64)

Love, Hope, Strength http://www.lovehopestrength.co.uk
User avatar
Shando
Skyline Moderator
Skyline Moderator
 
Posts: 448
Joined: 06 Mar 2013, 22:35
Location: Redland Bay, Queensland
Skill: Programmer
Skill: Scripter
Skill: Level Designer

Re: Optimisation Trick

Postby SolarPortal » 18 Jun 2015, 14:26

nice tricks. LUT's for speed are often the best and is the approach we take when programming in lua and shaders.
Most things can be done with an approximation. :)
Conceive, Quantify & Do!
Remember skyline is in beta, and with your help; we can improve it for future users.
Always give as much information as possible when asking for help.

Specs: OS: Win 10 64bit, CPU: Intel i7 3770 3.4ghz x 8 core, GPU: Nvidia GTX 750 Ti, Ram: 16gig DDR3
User avatar
SolarPortal
Skyline Founder
Skyline Founder
 
Posts: 2810
Joined: 29 Jul 2012, 15:56
Location: UK
Skill: 3D Modeller
Skill: 2D Artist
Skill: Programmer
Skill: Level Designer

Re: Optimisation Trick

Postby Shando » 17 Nov 2017, 07:12

Just a quick update on this....

Over the last few years, I've put together a list of some more Lua tricks that I've come across:

Using Memory Efficiently
1. Eliminate memory leaks — Global variables are never considered garbage so you need to set them to "nil" when they are no longer required.
2. Avoid loading resource files until they're actually needed.

Lua Optimizations

Localize, Localize
Avoiding Global variables and functions isn't always possible. Access to local variables and functions is faster, especially in time-critical routines.

This also applies to core Lua libraries. In time-critical routines, you should always localize library functions:

local sin = math.sin --local reference to 'math.sin'
local function foo(x)
for i = 1,100 do
x = x + sin(i)
end
return x
end

Finally, Functions should also be localized whenever possible (just remember to create them in the correct order):

--NON-LOCAL
function func1()
func2( "myValue" )
end

function func2( y )
print( y )
end

func1()

--LOCAL
local function func2( y ) --'func2' properly scoped above 'func1'
print( y )
end

local function func1()
func2( "myValue" )
end

func1()

Avoid "table.insert()"
table.insert() is quite slow and should be avoided. Below are a few alternatives:

--LOOP INDEX
local a = {}

for i = 1,100 do
a[i] = i
end

--TABLE SIZE
local a = {}

for i = 1,100 do
a[#a+1] = i
end

--COUNTER
local a = {}
local index = 1

for i = 1,100 do
a[index] = i
index = index+1
end

Avoid "unpack()"

unpack() is also slow, so use a Loop instead:

local a = { 100, 200, 300, 400 }

for i = 1,100 do
print( a[1],a[2],a[3],a[4] )
end

Avoid "ipairs()"

ipairs() has quite a high overhead, so should never really be used. A simple loop usually works just as well, with less overhead:

local t1 = {}
local t2 = {}
local t3 = {}
local t4 = {}
local a = { t1, t2, t3, t4 }

for i = 1,#a do
print( a[i] )
end

Math Performance

Multiplication is faster than division, so you should multiply by a decimal instead of dividing.
Multiplication is also faster than exponentiation:


Some of these tricks could be useful in time-critical sections of your code.

HTH

Shando
Alienware 17 (i7-4700MQ 2.4GHz 16GB GTX770M Win 10 64)

Love, Hope, Strength http://www.lovehopestrength.co.uk
User avatar
Shando
Skyline Moderator
Skyline Moderator
 
Posts: 448
Joined: 06 Mar 2013, 22:35
Location: Redland Bay, Queensland
Skill: Programmer
Skill: Scripter
Skill: Level Designer

Re: Optimisation Trick

Postby SpiderMack » 17 Nov 2017, 08:34

Shando wrote:Just a quick update on this....

Over the last few years, I've put together a list of some more Lua tricks that I've come across:

Using Memory Efficiently
1. Eliminate memory leaks — Global variables are never considered garbage so you need to set them to "nil" when they are no longer required.
2. Avoid loading resource files until they're actually needed.

Lua Optimizations

Localize, Localize
Avoiding Global variables and functions isn't always possible. Access to local variables and functions is faster, especially in time-critical routines.

This also applies to core Lua libraries. In time-critical routines, you should always localize library functions:

local sin = math.sin --local reference to 'math.sin'
local function foo(x)
for i = 1,100 do
x = x + sin(i)
end
return x
end

Finally, Functions should also be localized whenever possible (just remember to create them in the correct order):

--NON-LOCAL
function func1()
func2( "myValue" )
end

function func2( y )
print( y )
end

func1()

--LOCAL
local function func2( y ) --'func2' properly scoped above 'func1'
print( y )
end

local function func1()
func2( "myValue" )
end

func1()

Avoid "table.insert()"
table.insert() is quite slow and should be avoided. Below are a few alternatives:

--LOOP INDEX
local a = {}

for i = 1,100 do
a[i] = i
end

--TABLE SIZE
local a = {}

for i = 1,100 do
a[#a+1] = i
end

--COUNTER
local a = {}
local index = 1

for i = 1,100 do
a[index] = i
index = index+1
end

Avoid "unpack()"

unpack() is also slow, so use a Loop instead:

local a = { 100, 200, 300, 400 }

for i = 1,100 do
print( a[1],a[2],a[3],a[4] )
end

Avoid "ipairs()"

ipairs() has quite a high overhead, so should never really be used. A simple loop usually works just as well, with less overhead:

local t1 = {}
local t2 = {}
local t3 = {}
local t4 = {}
local a = { t1, t2, t3, t4 }

for i = 1,#a do
print( a[i] )
end

Math Performance

Multiplication is faster than division, so you should multiply by a decimal instead of dividing.
Multiplication is also faster than exponentiation:


Some of these tricks could be useful in time-critical sections of your code.

HTH

Shando


This is usefull, you should pin up that on Lua Scripting and in some wiki documentation.
User avatar
SpiderMack
Skyline Warrior
 
Posts: 346
Joined: 02 Dec 2016, 09:15
Skill: Concept artist
Skill: 3D Modeller
Skill: Level Designer
Skill: Scripter

Re: Optimisation Trick

Postby SolarPortal » 17 Nov 2017, 15:10

Added to my list of todo jobs :)
Conceive, Quantify & Do!
Remember skyline is in beta, and with your help; we can improve it for future users.
Always give as much information as possible when asking for help.

Specs: OS: Win 10 64bit, CPU: Intel i7 3770 3.4ghz x 8 core, GPU: Nvidia GTX 750 Ti, Ram: 16gig DDR3
User avatar
SolarPortal
Skyline Founder
Skyline Founder
 
Posts: 2810
Joined: 29 Jul 2012, 15:56
Location: UK
Skill: 3D Modeller
Skill: 2D Artist
Skill: Programmer
Skill: Level Designer


Return to Lua Scripting

Who is online

Users browsing this forum: No registered users and 1 guest