Tiramisu
Warlord
Currently I am trying to create an A*-pathfinding Algorithm in Lua, but I am having huge performance problems.
So now I wonder which Lua operations take the most calculation time.
I have found out that using tables too many times can cost a huge amount of performance, so you should better use numbers instead of tables if possible (e.g. using the Index number of a plot or its coordinates instead of the Lua object of the plot itself). This insight is kind of obvious.
However, there are many other details that I wanted to find out about performance in Lua. So I have created the following test function, which I am using in Firetuner as an action control:
It works like this: First I put some code in place of
Then I take a look at the results in the log to see how many times this code could be executed within iDeltaSeconds, which is 1 second here.
At first I have tested the following code:
When I let this test run several times the ouput looks like this:
So the number n of iteration steps within one second can vary, but the maximum is about 370.000 steps.
In my second test I wondered, how much performance it would save, if I declared some of the variables outside of the while loop:
In this case Firetuner can execute about 460.000 steps in one second, which is 25% more than 370.000 steps. So I conclude from this two tests that declaring variables within a loop can cause much more performance cost.
I will do further tests and try to collect more informations about how to save performance in Lua. Your help is appreciated, if you have your own suggestions.
So now I wonder which Lua operations take the most calculation time.
I have found out that using tables too many times can cost a huge amount of performance, so you should better use numbers instead of tables if possible (e.g. using the Index number of a plot or its coordinates instead of the Lua object of the plot itself). This insight is kind of obvious.
However, there are many other details that I wanted to find out about performance in Lua. So I have created the following test function, which I am using in Firetuner as an action control:
Code:
local iDeltaSeconds = 1
local x = os.clock()
local n = 0
while os.clock() < x + iDeltaSeconds do
n = n + 1
--------------------
--insert test code here
--------------------
end
print("n: " .. n)
Code:
--insert test code here
At first I have tested the following code:
Spoiler :
Code:
local iDeltaSeconds = 1
local x = os.clock()
local n = 0
while os.clock() < x + iDeltaSeconds do
n = n + 1
--------------------
local pPlayer = Players[0];
local pUnits = pPlayer:GetUnits();
local pPlot
for ii, pUnit in pUnits:Members() do
pPlot = Map.GetPlot(pUnit:GetX(), pUnit:GetY())
end
--------------------
end
print("n: " .. n)
When I let this test run several times the ouput looks like this:
Spoiler :
So the number n of iteration steps within one second can vary, but the maximum is about 370.000 steps.
In my second test I wondered, how much performance it would save, if I declared some of the variables outside of the while loop:
Spoiler :
Code:
local iDeltaSeconds = 1
local x = os.clock()
local n = 0
local pPlayer = Players[0];
local pUnits = pPlayer:GetUnits();
local pPlot
while os.clock() < x + iDeltaSeconds do
n = n + 1
--------------------
for ii, pUnit in pUnits:Members() do
pPlot = Map.GetPlot(pUnit:GetX(), pUnit:GetY())
end
--------------------
end
print("n: " .. n)
In this case Firetuner can execute about 460.000 steps in one second, which is 25% more than 370.000 steps. So I conclude from this two tests that declaring variables within a loop can cause much more performance cost.
I will do further tests and try to collect more informations about how to save performance in Lua. Your help is appreciated, if you have your own suggestions.
Attachments
Last edited: