金庆的专栏

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  423 随笔 :: 0 文章 :: 454 评论 :: 0 Trackbacks
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
posted on 2017-08-30 17:25 金庆 阅读(1098) 评论(0)  编辑 收藏 引用 所属分类: 1. C/C++2. 网游开发

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理