Page 1 of 1

Profiling your code

Posted: Fri Apr 23, 2021 8:47 am
by sisyphu2
Hi all, noob question but what (in simple terms) is the best way in love to profile/analyse my code to see which parts need optimising?

My prototype project so far has around 9-10,000 lines so it's pretty extensive now and mostly runs fine but I know it could be better.

Thanks in advance for any tips!

Re: Profiling your code

Posted: Fri Apr 23, 2021 1:18 pm
by Gunroar:Cannon()
I recommended ProFi. It's simple and it's easy to find which functions take the longest to run.
Just use ProFi:start() at the start of the function you want to profile(even if it's at the beginning of love.update) then call ProFi:stop() at the line of the function where you want to stop profiling.
After that do ProFi:writeReport("filename.txt") to get the report printed into a file in nice columns ^^ .
Though I had to change the filesystem in ProFi.lua to use love.filesystem instead of io.

Re: Profiling your code

Posted: Sat Apr 24, 2021 9:56 am
by sisyphu2
Gunroar:Cannon() wrote: Fri Apr 23, 2021 1:18 pm I recommended ProFi. It's simple and it's easy to find which functions take the longest to run.
Just use ProFi:start() at the start of the function you want to profile(even if it's at the beginning of love.update) then call ProFi:stop() at the line of the function where you want to stop profiling.
After that do ProFi:writeReport("filename.txt") to get the report printed into a file in nice columns ^^ .
Though I had to change the filesystem in ProFi.lua to use love.filesystem instead of io.
That's really helpful thank you, I'll try that now! :ultraglee:

Re: Profiling your code

Posted: Wed Apr 28, 2021 2:41 am
by RNavega
The example over at the wiki page for love.timer.getTime() shows how to measure the time taken to run a piece of code.

Note that this includes the time taken for everything else being done by the CPU. If you run this profiling code while the CPU is busy doing something else and then run it again when it's free, the results will be different.
So taking reference from the Python 'timeit' module, do the measurement several times and only consider the smallest/fastest one.

Based on that example in the wiki, it'd be something like this:

Code: Select all

local result = math.huge

-- Measure it 5 times, consider only the fastest time.
for attempts = 1, 5 do
  
  local start = love.timer.getTime()
  
  local foo = ""
  for _ = 1, 1000 do
    -- The function / piece of code you're trying to profile.
    foo = foo .. "bar"
  end
  
  local tempResult = love.timer.getTime() - start
  if tempResult < result then
    result = tempResult
  end
end

print(string.format("It took %.3f milliseconds to concatenate 'bar' 1000 times!", result * 1000))

Re: Profiling your code

Posted: Wed Apr 28, 2021 12:11 pm
by zorg

Re: Profiling your code

Posted: Thu Apr 29, 2021 7:26 pm
by sisyphu2
Thanks both, this has helped demystify the concept of profiling for me greatly.