Simple Bench for Corona SDK

I didn’t have the time to write an article on Monday, so I’m writing two related articles today.

This article introduces a simple benchmarking module which you can download here and use in your projects.

Including The Module

To include the module in your project, simply download the example and copy the file ‘simpleBench.lua’ into your own project. Then add this line to any files that need to use the module:

local bench = require "simpleBench"

Measuring Stuff

Current Memory Usage and Deltas

The following example measures the initial memory usage, builds a table with 1,000,00 entries, and then measures current memory usage and the delta (difference):

local bench = require "simpleBench"
local round = bench.round
--
-- Test cost of table with 1,000,000 integer entries
-- 

-- Call once to prepare memory count
local mem,delta = bench.getMemCount()
print( "Starting memory usage == " .. round(mem,2) .. " KB")

-- Build and fill the table
local tmp = {}

for i = 1, 1000000 do
   tmp[i] = i 
end

-- Get current memory usage and delta since last measurement (in KB)
local mem,delta = bench.getMemCount()
print( "Current memory usage == " .. round(mem,2) .. " KB")
print( "Delta memory usage == " .. round(delta/1024,2) .. " MB (cost of 1,000,000 entry table)")

Time To Execute A Function

You can also measure how long it takes to execute any arbitrary function once,

local bench = require "simpleBench"
local function myTest() 
   -- Do something here
end

local time = bench.measureTime( myTest )

print( "myTest() took", time, " milliseconds to run.") 

, or multiple times:

 local time = bench.measureTime( myTest, 10000 )

print( "myTest() took", time, " milliseconds to run 10,000 times.") 

Time To Execute Functions – AB Testing

You can also compare two functions’ runtimes to each other:

local bench = require "simpleBench"
local round = bench.round

--
-- Test math.sqrt vs localized function.
-- 

-- Slow version
local function test1( iter)
   for i = 1, 100000 do
      math.sqrt( i )
   end
end

-- Faster Version
local mSqrt = math.sqrt
local function test2()
   for i = 1, 100000 do
      mSqrt( i )
   end
end

-- Measuring attempt 1 (one iteration per test)
--
local time1,time2,delta,speedup = bench.measureABTime( test1, test2 )
print( "\nSingle run 100,000 calculations.")
print( "Test 1: " .. round(time1/1000,2) .. " seconds.")
print( "Test 2: " .. round(time2/1000,2) .. " seconds.")
print( "Test 2 is " .. speedup .. " percent faster .")
if( speedup == 0 ) then
   print( "Tests may have run too fast to measure appreciable speedup.")
end

-- Measuring attempt 2 (100 iteration per test)
--
local time1,time2,delta,speedup = bench.measureABTime( test1, test2, 100 )
print( "\n100 runs x 100,000 calculations each.")
print( "Test 1: " .. round(time1/1000,2) .. " seconds.")
print( "Test 2: " .. round(time2/1000,2) .. " seconds.")
print( "Test 2 is " .. speedup .. " percent faster .")

-- Measuring attempt 3 (200 iteration per test)
--
local time1,time2,delta,speedup = bench.measureABTime( test1, test2, 200 )
print( "\n200 runs x 100,000 calculations each.")
print( "Test 1: " .. round(time1/1000,2) .. " seconds.")
print( "Test 2: " .. round(time2/1000,2) .. " seconds.")
print( "Test 2 is " .. speedup .. " percent faster .")

-- Measuring attempt 4 (300 iteration per test)
--
local time1,time2,delta,speedup = bench.measureABTime( test1, test2, 300 )
print( "\n300 runs x 100,000 calculations each.")
print( "Test 1: " .. round(time1/1000,2) .. " seconds.")
print( "Test 2: " .. round(time2/1000,2) .. " seconds.")
print( "Test 2 is " .. speedup .. " percent faster .")

Please note, I will be using this module in future articles.

Assisted by App Store Assistant