diff --git a/performance/run.lua b/performance/run.lua index ecc5fcd..4fa0e55 100644 --- a/performance/run.lua +++ b/performance/run.lua @@ -3,5 +3,41 @@ require 'middleclass' time = require 'performance/time' time('class creation', function() - local c = class('A') + local A = class('A') +end) + +local A = class('A') + +time('instance creation', function() + local a = A:new() +end) + +function A:foo() + return 1 +end + +local a = A:new() + +time('instance method invocation', function() + a:foo() +end) + +local B = class('B', A) + +local b = B:new() + +time('inherited method invocation', function() + b:foo() +end) + +function A.static:bar() + return 2 +end + +time('class method invocation', function() + A:bar() +end) + +time('inherited class method invocation', function() + B:bar() end) diff --git a/performance/time.lua b/performance/time.lua index e243afa..dd02455 100644 --- a/performance/time.lua +++ b/performance/time.lua @@ -1,9 +1,13 @@ return function(title, f) - local start = os.clock() + collectgarbage() + + local startTime = os.clock() for i=0,10000 do f() end - print( title, os.clock() - start ) + local endTime = os.clock() + + print( title, endTime - startTime ) end