Lua和C++之间调用效率测试

Lua和C++之间调用效率测试

(金庆的专栏 2017.8)

仿照 http://www.cnblogs.com/archy_yu/p/3185608.html 对 Lua 和 C++ 调用进行测试。

代码见:https://github.com/jinq0123/TimerLuaIntf

使用 LuaIntf 绑定 Lua 和 C++。用 boost timer 计时。
依赖库 lua-cpp, lua-intf, boost-timer 用 conan 安装。
conan 会下载源码,编译,然后生成 conanbuildinfo.props 给 VS 导入,
其中设好了所有 include, lib 目录,链接库,运行库。

代码大概如下:

cout << "C++ calls lua add() many times:\n";
{
    boost::timer::auto_cpu_timer t;
    for (int i = 0; i < COUNT; ++i)
        test.dispatchStatic("add", 123, 456);
}
cout << "C++ calls lua add_times() once:\n";
{
    boost::timer::auto_cpu_timer t;
    test.dispatchStatic("add_times", 123, 456, COUNT);
}
cout << "Lua calls C++ add() many times:\n";
{
    boost::timer::auto_cpu_timer t;
    test.dispatchStatic("test_c_add", 123, 456, COUNT);
}
cout << "Lua calls C++ add_times() once:\n";
{
    boost::timer::auto_cpu_timer t;
    test.dispatchStatic("test_c_add_times", 123, 456, COUNT);
}

测试4种调用:

  • C++ 调用 1kw 次 lua add()
  • C++ 调用 1 次 lua add_times(), 其中调用 add() 1kw 次
  • Lua 调用 C++ add() 1kw 次
  • Lua 调用 C++ add_times() 1 次,其中调用 add() 1kw 次

输出如:

C++ calls lua add() many times:
 2.759473s wall, 2.761218s user + 0.000000s system = 2.761218s CPU (100.1%)
C++ calls lua add_times() once:
 0.436400s wall, 0.436803s user + 0.000000s system = 0.436803s CPU (100.1%)
Lua calls C++ add() many times:
 0.535802s wall, 0.530403s user + 0.000000s system = 0.530403s CPU (99.0%)
Lua calls C++ add_times() once:
 0.000005s wall, 0.000000s user + 0.000000s system = 0.000000s CPU (n/a%)

结论是:

  • C++ 调用 Lua 可达 3百万次/s
  • Lua 内部调用函数可达 2千万次强/s
  • Lua 调用 C++ 函数可达 2千万次弱/s
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值