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!
Profiling your code
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- Gunroar:Cannon()
- Party member
- Posts: 1143
- Joined: Thu Dec 10, 2020 1:57 am
Re: Profiling your code
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.
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
That's really helpful thank you, I'll try that now!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.
Re: Profiling your code
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:
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))
- zorg
- Party member
- Posts: 3468
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: Profiling your code
There's also jprof: https://github.com/pfirsich/jprof
Me and my stuff True 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.
Re: Profiling your code
Thanks both, this has helped demystify the concept of profiling for me greatly.
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot], Semrush [Bot] and 6 guests