Skip to content

Instantly share code, notes, and snippets.

@Shchvova
Last active July 14, 2016 17:30
Show Gist options
  • Save Shchvova/90b60ed034dcf34ca27cff19ca3184aa to your computer and use it in GitHub Desktop.
Save Shchvova/90b60ed034dcf34ca27cff19ca3184aa to your computer and use it in GitHub Desktop.
Corona FPS Benchmark

This code will declare global function BenchmarkFPS(). When called it will record frame duration statistics over next 5 seconds after skipping half a second. Duration and skip interval can be overridden if passed as optional parameters (in milliseconds).

Sample output:

Jul 04 12:34:56.789 ------------------------------------------------------------
                    Mean FPS is 62.50 measured in 281 frames
                    with mean frame time 16.00ms, standard deviation of 0.70ms (0.25%)
                    99% of all frames took between 15.89ms and 16.11ms to render
                    ------------------------------------------------------------
-- this code will introduce global function BenchmarkFPS
-- which would print out some statistics collected over 5 seconds
function BenchmarkFPS(duration, skip)
duration = duration or 5000
skip = skip or 500
local function mean( t )
local sum , count = 0, 0
for _,v in pairs(t) do
sum = sum + v
count = count + 1
end
return (sum / count)
end
local function std( t )
local mean, sum, count = mean( t ), 0, 0
for _,v in pairs(t) do
local vm = v - mean
sum = sum + (vm * vm)
count = count + 1
end
return math.sqrt(sum / (count-1))
end
local fps = {}
local prevTime = 0
function onFrame ( event )
local t = event.time
if t < skip then -- skip first second
elseif t > duration then -- measure fps for 5 seconds
local n,sigma, x_ = #fps, std(fps), mean(fps)
local normSigma = 100*sigma/n
local confRange = 2.57*sigma/math.sqrt( n )
local confL, confH = x_ - confRange, x_ + confRange
local res = ""
.. "------------------------------------------------------------\n"
..string.format("Mean FPS is %.2f measured in %d frames\n", 1000/x_, n )
..string.format("with mean frame time %.2fms, standard deviation of %.2fms (%.2f%%)\n", x_, sigma, normSigma)
..string.format("99%% of all frames took between %.2fms and %.2fms to render\n", confL, confH)
.. "------------------------------------------------------------"
print(res)
Runtime:removeEventListener( "enterFrame", onFrame )
else
fps[#fps+1] = t - prevTime
end
prevTime = t
end
Runtime:addEventListener( "enterFrame", onFrame )
end
-- start benchmark
BenchmarkFPS()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment