Open
Description
Hi, I was trying to benchmark mlua to see what gain I can achieve by moving some of the logic of my program to mlua so I wrote this test:
use mlua::prelude::*;
fn sum(_lua: &Lua, t: LuaTable) -> LuaResult<f64> {
let mut ret: f64 = 0.;
for t in t.sequence_values::<LuaTable>() {
let t = t.unwrap();
let v: f64 = t.get("count").unwrap();
ret += v;
}
Ok(ret)
}
#[mlua::lua_module]
fn tmod(lua: &Lua) -> LuaResult<LuaTable> {
let exports = lua.create_table()?;
exports.set("sum", lua.create_function(sum)?)?;
Ok(exports)
}
I wrote this Lua code to benchmark it:
package.path = package.path .. ";.\\target\\release\\?.lua;"
package.cpath = package.cpath .. ";.\\target\\release\\?.dll;"
function sum_lua(l)
local ret = 0
for k, v in pairs(l) do
ret = ret + v["count"]
end
return ret
end
function gen_data()
local ret = {}
for i = 1, 10000 do
ret[#ret + 1] = { count = i }
end
return ret
end
function mar(func)
local totalRuntime = 0
local ret = 0
local testTimes = 100
for i = 1, testTimes do
local startTime = os.clock()
ret = ret + func()
local endTime = os.clock()
totalRuntime = totalRuntime + (endTime - startTime)
end
print(ret)
local averageRuntime = totalRuntime / testTimes
print("Average runtime for 1000 executions: " .. averageRuntime .. " seconds")
return averageRuntime
end
local data = gen_data()
tmod= require("tmod")
testsum = tmod.sum
print(sum_lua(data))
print(testsum(data))
local b = mar(function()
return testsum(data)
end)
local a = mar(function()
return sum_lua(data)
end)
print(b / a)
And i got this result:
50005000
50005000
5000500000
Average runtime for 1000 executions: 0.00559 seconds
5000500000
Average runtime for 1000 executions: 0.00038 seconds
14.710526315789
This means Rust code is doing the same task 14 times slower than Lua code.
I wonder what caused that, I'm not really familiar with Lua internals. so I can't say if it's a bug or not.
Target Lua version: Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio
Rust version: rustc 1.72.1 (d5c2e9c34 2023-09-13)
Metadata
Metadata
Assignees
Labels
No labels