与访问cdata字段相比,访问Lua表字段要快吗?

问题描述 投票:0回答:1

说我有一个cdata变量(FFI查询的结果)。然后,将其字段复制到Lua表中。更快的方法是:Lua表或原始cdata变量的访问字段?

lua luajit
1个回答
0
投票

让我们检查一下

使用jit:

./tarantool -l clock -l ffi
Tarantool 2.3.1-18-ga5a2bb28a
type 'help' for interactive help
tarantool> ffi.cdef[[ typedef struct test { int field; } ]]
---
...

tarantool> T = ffi.typeof('struct test')
---
...

tarantool> F = T({12345})
---
...

tarantool> L = {field = 12345}
---
...
tarantool> local st = clock.proc() for i=1,1e8 do local x = F.field end return clock.proc() - st
---
- 0.036895
...

tarantool> local st = clock.proc() for i=1,1e8 do local x = L.field end return clock.proc() - st
---
- 0.037841
...

并且没有jit:

tarantool> jit.off()
---
...
tarantool> local st = clock.proc() for i=1,1e6 do local x = F.field end return clock.proc() - st
---
- 0.198209
...
tarantool> local st = clock.proc() for i=1,1e6 do local x = L.field end return clock.proc() - st
---
- 0.010116
...
© www.soinside.com 2019 - 2024. All rights reserved.