## Measuring Lua performance

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
kikito
Inner party member
Posts: 3142
Joined: Sat Oct 03, 2009 5:22 pm
Contact:

### Measuring Lua performance

I'd like to compare two different implementations of an algorithm and I'm trying to come out with a satisfactory way of timing pieces of Lua code easily.

This is what I've got so far:

Code: Select all

-- time.lua
return function(title, f)

collectgarbage()

local startTime = os.clock()

for i=0,10000 do f() end

local endTime = os.clock()

print( title, endTime - startTime )

end

It basically uses os.clock for measuring time in milliseconds, while repeating the same piece of code 10k times. This is how you use it:

Code: Select all

local time = require 'time'

time('first implementation', function()
-- do stuff here
end)

time('second implementation, function()
-- do other stuff here
end)

Output:

Code: Select all

first implementation     0.1
second implementation 0.01

I'd like to know your opinion; do you see any obvious problem with this implementation? What would you have done differently?

Thanks!
When I write def I mean function.

GijsB
Party member
Posts: 379
Joined: Wed Jul 20, 2011 10:19 pm
Location: Netherlands

### Re: Measuring Lua performance

I think that that is the best way to do it.

Citizen
Posts: 69
Joined: Mon Aug 08, 2011 12:28 pm

### Re: Measuring Lua performance

One drawback of this approach is that it only tells you how much time passed between starting and finishing a certain task, but this can depend on what the other programs (and OS) are doing (edit: ignore this - not true). On Linux (maybe Mac too) you can use time command. It measures only the time CPU spent executing the command you specified.
Last edited by Rad3k on Sat Sep 10, 2011 2:52 pm, edited 2 times in total.

benloran
Prole
Posts: 19
Joined: Tue Jul 05, 2011 4:52 pm

### Re: Measuring Lua performance

Depending on what you're measuring, you may want to consider disabling the garbage collector completely with collectgarbage('stop') at the beginning. Otherwise, that's exactly how I do it.

You could also look into using something like luaProfiler or pepperfish (the latter is much easier to set up).

Xgoff
Party member
Posts: 211
Joined: Fri Nov 19, 2010 4:20 am

### Re: Measuring Lua performance

kikito wrote:I'd like to compare two different implementations of an algorithm and I'm trying to come out with a satisfactory way of timing pieces of Lua code easily.

This is what I've got so far:

Code: Select all

-- time.lua
return function(title, f)

collectgarbage()

local startTime = os.clock()

for i=0,10000 do f() end

local endTime = os.clock()

print( title, endTime - startTime )

end

It basically uses os.clock for measuring time in milliseconds, while repeating the same piece of code 10k times. This is how you use it:

Code: Select all

local time = require 'time'

time('first implementation', function()
-- do stuff here
end)

time('second implementation, function()
-- do other stuff here
end)

Output:

Code: Select all

first implementation     0.1
second implementation 0.01

I'd like to know your opinion; do you see any obvious problem with this implementation? What would you have done differently?

Thanks!
it probably wouldn't hurt to subtract from those the runtime of a loop calling a (localized) empty function with the same number of iterations. for some things you might be timing the function call overhead could be fairly significant

vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

### Re: Measuring Lua performance

Rad3k wrote:One drawback of this approach is that it only tells you how much time passed between starting and finishing a certain task
os.clock() measures CPU time, so it's equivalent to the time command.
For simple profiling this seems to be the best approach. However, I would add a relative comparison of the algorithms.
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine

Citizen
Posts: 69
Joined: Mon Aug 08, 2011 12:28 pm

### Re: Measuring Lua performance

vrld wrote: os.clock() measures CPU time, so it's equivalent to the time command.
I didn't know about that - my bad, I should have checked it myself.

kikito
Inner party member
Posts: 3142
Joined: Sat Oct 03, 2009 5:22 pm
Contact:

### Re: Measuring Lua performance

I must have forgotten to press the "Submit" button.

Thanks for all the feedback. I still need to do some more tests, but it's good to know that the path I'm walking looks correct.

Regards!
When I write def I mean function.

### Who is online

Users browsing this forum: Bing [Bot] and 9 guests