我真的需要在Lua中有一個整數類型。Lua整數類型
我的意思是整數類型是一個類型定義通常的操作符(/ * +等)和行爲像一個整數,內部表示並不重要。
用桌子做這樣的事情很簡單,問題是,我嘗試過,而且表現非常差(當然)。這是我的部分實現:
function num_op(a, b, calc_func)
local atype = pytype(a)
local btype = pytype(b)
local a_val, b_val
a_val = (atype == "Integer" or atype == "Double") and a[1] or a
b_val = (btype == "Integer" or btype == "Double") and b[1] or b
val = calc_func(a_val, b_val)
if atype == "Integer" and btype == "Integer" then
return Integer:create(val)
else
return Double:create(val)
end
end
numeric_mt = {
__add = function(a, b)
return num_op(a, b, function(a,b) return a + b end)
end,
__sub = function(a, b)
return num_op(a, b, function(a,b) return a - b end)
end,
__div = function(a, b)
return num_op(a, b, function(a,b) return a/b end)
end,
__mul = function(a, b)
return num_op(a, b, function(a,b) return a * b end)
end,
__tostring = function(a)
return tostring(a[1])
end
}
-----------------------------
-- Integer type definition --
-----------------------------
Integer = {}
Integer_mt = table.copy(numeric_mt)
Integer_mt["__index"] = Integer
function Integer:create(value)
local new_inst = {math.floor(value)}
setmetatable(new_inst, Integer_mt)
return new_inst
end
function Integer:className()
return "Integer"
end
我收集的主要性能損失是(當然)非常多的分配。 LuaJit能夠很好地優化運算符功能,但不能實現metatables分配。
難道有人認爲使用自定義c實現和userdata可以做得更好嗎?或者是我追求不可能達到的目標?
注:i 知道 lua沒有整數。我也知道,我可以使用數學庫獲得相同的結果。我想要的是完整使用整數時的透明度,除了創建階段。
編輯:我會在這裏添加額外的信息,以便一切仍是集中
@Mud:我需要,在一定程度上有透明混合算術中你在Python具有相同的方式/ ruby /等,但是可能具有最佳性能。我使用luaJIT作爲編譯器的目標,將常規Lua用作luaJIT不支持的平臺的後備。這對於性能特徵非常重要。
這意味着,我想能夠做到這一點:
a = int(5) -- Integer value
b = int(2) -- Another Integer
c = 2 -- Double
d = a/b -- == 2 , integer arithmetics
e = a/c -- == 2.5, floating point arithmetics
我能達到這個到某一點,與實施上述表現。問題在於,我對每個數字的運算速度都很慢,因爲常規數字也被裝箱。我可以超載與調試LIB號的元表,但
- 我不知道這個功能如何可靠的是在生產高質量的軟件使用
- 它將數字以來的表現還是慢下來,能夠有一個統一的數字接口,我將不得不使用(數字):get(),這將在任何情況下減緩操作。
我在昨晚的C中推出了自己的Integer實現。問題是,雖然它比普通的lua中我的天真實現有所改進,並且還改進了對math.floor的內聯調用,但使用LuaJIT時的情況並不那麼明顯,在這種情況下,內聯調用仍然是比C更快的批次實現。
另一種解決方案是始終使用unboxed數字,並在我的編譯器中使用某種類型的傳播來跟蹤整數,並在需要時使用適當的內聯操作,但此解決方案的複雜性要大得多,擊敗了使用Lua/LuaJIT作爲後端的全部目的。
我會嘗試你的實現,但我懷疑它會比在LuaJIT中的內聯調用更好。很可能,我所拍攝的內容(既有雙精度和整數的透明操作,又有接近對luaJIT的內聯調用的性能)顯然是不可能的。非常感謝您的幫助。
@miky:謝謝,這看起來不錯,但我懷疑我可以用它補丁luaJIT,如果我不能,它會爲我的目標放鬆所有的Interrest。
是的,這是真的,想用什麼來修補LuaJIT是瘋狂,它始終是更好地實現純Lua的整個事情(當然,這就是Lua是很好的,它提供的工具.. )。然後LuaJIT將能夠優化它(而不是C函數)。 – 2010-12-21 16:21:23