Skip to content

Instantly share code, notes, and snippets.

@jamesu
Last active August 29, 2015 13:56
Show Gist options
  • Save jamesu/8801175 to your computer and use it in GitHub Desktop.
Save jamesu/8801175 to your computer and use it in GitHub Desktop.
Torque2D-Lua object thunk benchmarking
function bridge1Test()
{
%obj = new SimObject();
%start = getRealTime();
%count = 0;
for (%i=0; %i<1000000; %i++) {
%obj.getId();
}
%end = getRealTime();
//echo("scriptTest3 result: " @ %count);
return %end - %start;
}
function bridge2Test()
{
%obj = new SimObject();
%start = getRealTime();
%count = 0;
for (%i=0; %i<1000000; %i++) {
%obj.prop = "123";
}
%end = getRealTime();
//echo("scriptTest3 result: " @ %count);
return %end - %start;
}
function bridge3Test()
{
%obj = new SimObject();
%start = getRealTime();
%count = 0;
for (%i=0; %i<1000000; %i++) {
%obj.setFieldValue("prop", "123");
}
%end = getRealTime();
//echo("scriptTest3 result: " @ %count);
return %end - %start;
}
function runAndPrintTimesFor(%test)
{
%times = %test;
%count = 0;
for (%i=0; %i<5; %i++) {
eval("$time = " @ %test @ "();");
%times = %times @ "," @ $time;
%count += $time;
}
%mean = %count / 5.0;
echo(%times @ "," @ %mean);
}
runAndPrintTimesFor("bridge1Test");
runAndPrintTimesFor("bridge2Test");
runAndPrintTimesFor("bridge3Test");
-- Lua test script
function bridge1Test()
local obj = AssetBase()
local count = 0
local start = os.clock()
for i=1,1000000 do
obj:getId()
end
return os.clock() - start
end
function bridge2Test()
local obj = AssetBase()
local count = 0
local start = os.clock()
for i=1,1000000 do
obj.prop = '123'
end
return os.clock() - start
end
function bridge3Test()
local obj = AssetBase()
local count = 0
local start = os.clock()
for i=1,1000000 do
obj:setFieldValue('prop', '123')
end
return os.clock() - start
end
gtime = 0
function runTest(testName)
io.write(testName)
local count = 0
for i=1,5 do
load("gtime = " .. testName .. "()")()
count = count + (gtime*1000.0)
io.write(string.format(",%f", gtime*1000.0))
end
io.write(string.format(",%f\n", count / 5.0))
end
print("TEST,1,2,3,4,5,AVG")
runTest("bridge1Test")
runTest("bridge2Test")
runTest("bridge3Test")
@crabmusket
Copy link

Surely eval("$time = " @ %test @ "();"); could be $time = call(%test);. Ok, that's my drive-by code sniping for the evening :P.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment