Profiling your code

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
sisyphu2
Prole
Posts: 17
Joined: Mon Sep 28, 2020 3:15 pm

Profiling your code

Post 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!
User avatar
Gunroar:Cannon()
Party member
Posts: 1085
Joined: Thu Dec 10, 2020 1:57 am

Re: Profiling your code

Post 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.
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
sisyphu2
Prole
Posts: 17
Joined: Mon Sep 28, 2020 3:15 pm

Re: Profiling your code

Post 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:
RNavega
Party member
Posts: 235
Joined: Sun Aug 16, 2020 1:28 pm

Re: Profiling your code

Post 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))
User avatar
zorg
Party member
Posts: 3435
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Profiling your code

Post by zorg »

Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
sisyphu2
Prole
Posts: 17
Joined: Mon Sep 28, 2020 3:15 pm

Re: Profiling your code

Post by sisyphu2 »

Thanks both, this has helped demystify the concept of profiling for me greatly.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 15 guests