lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Dibyendu Majumdar <mobile@majumdar.org.uk> 于2020年8月20日周四 上午1:12写道:
> In the early days of Ravi I checked performance of matrix
> multiplication with Lua tables versus C arrays with metamethods.
> Even without metatable checking the C arrays were slower than Lua
> tables - metamethods are costly to invoke!
> Adding metatable checking made it even worse of course (I was using
> the optimized version suggested by Luiz in the mailing list a long
> while ago).
>
> I think it is best to benchmark the perceived advantage first. You may
> be surprised.

I have tried to make a cache for metamethods today, and do some simple
benchmark.

```lua
local N = 100000000

local a = {}
local meta = {} ; meta.__index = meta

function meta:foo()
end

setmetatable(a, meta)

local t = os.clock()

for i = 1, N do
  local foo = a.foo
end

local t1 = os.clock()

for i = 1, N do
  local foo = a:foo()
end

local t2 = os.clock()

print(t1 - t, t2 - t1)
```

Intel i7-7700 @ 3.60GHz Windows 10 mingw64 gcc 8.2.0

lua 5.4 (https://github.com/lua/lua master) 1.524   2.887
After patch (See attachment):                     1.271   2.492

This patch is only a prototype for benchmark.

Attachment: metacache.patch
Description: Binary data