做cocos2d-x开发的人可能有不少人在实现类时会利用cocos2d-x自己给出的类的实现,也即在luaBinding目录下extern.lua的文件中给出的实现:
--Create an class. function class(classname, super) local superType = type(super) local cls if superType ~= "function" and superType ~= "table" then superType = nil super = nil end if superType == "function" or (super and super.__ctype == 1) then -- inherited from native C++ Object cls = {} if superType == "table" then -- copy fields from super for k,v in pairs(super) do cls[k] = v end cls.__create = super.__create cls.super = super else cls.__create = super end cls.ctor = function() end cls.__cname = classname cls.__ctype = 1 function cls.new(...) local instance = cls.__create(...) -- copy fields from class to native object for k,v in pairs(cls) do instance[k] = v end instance.class = cls instance:ctor(...) return instance end else -- inherited from Lua Object if super then cls = clone(super) cls.super = super else cls = {ctor = function() end} end cls.__cname = classname cls.__ctype = 2 -- lua cls.__index = cls function cls.new(...) local instance = setmetatable({}, cls) instance.class = cls instance:ctor(...) return instance end end return cls end
上面这样说是因为这里的做法:
function cls.new(...) --instance是一个userdata local instance = cls.__create(...) -- copy fields from class to native object for k,v in pairs(cls) do instance[k] = v end instance.class = cls instance:ctor(...) return instance end
----------------------
测试代码1(o为table,val为table中变量):
local o = {} o.val = 1 local t1 = os.clock() for i = 1, 10000000, 1 do o.val = o.val + 1 end local t2 = os.clock() print(t2 - t1)多次运行,打印纸稳定在0.01数量级。
----------------------
测试代码2(o为userdata,val为userdata中变量):
local o = cc.Node:create() o.val = 1 local t1 = os.clock() for i = 1, 10000000, 1 do o.val = o.val + 1 end local t2 = os.clock() print(t2 - t1)多次运行,打印纸稳定在4.0数量级。
******************
测试发现,前者的效率为后者的近400倍,也即在table中访问对象比在userdata中访问,速度有近400倍的提升。
因此,使用cocos2d-x给出的类的实现,虽然能做到支持继承cocos2d-x中的c++对象,但该机制的性能是值得注意的。
原因分析(这里只是猜测,因为暂未看lua及tolua++的实现):