sol2/bench_lua_function.hpp
ThePhD 1d93f560f2 more luajit compatibility fixes - works with Lua 5.1, 5.2, 5.3
additional tests to make sure pass-by-value and copy semantics work as intended
new proxy_base class to reduce code duplication
update function / protected_function usage (to solve starwing's issue while keeping code as clean as possible)
2016-02-01 03:27:06 -05:00

93 lines
2.7 KiB
C++

#pragma once
#include "sol.hpp"
#include "nonius/nonius.h++"
static sol::state prepare_lua_function_state( ) {
sol::state lua;
lua.script( R"(
function r ()
return 1 + 1
end
)" );
return lua;
}
struct sol_function_result_lua_bench {
void operator () ( nonius::chronometer meter ) const {
sol::state lua = prepare_lua_function_state( );
auto measurement = [ & ] ( int run_index ) {
sol::function r = lua[ "r" ];
int value = r( );
return value;
};
meter.measure( measurement );
}
};
struct sol_call_lua_bench {
void operator () ( nonius::chronometer meter ) const {
sol::state lua = prepare_lua_function_state( );
auto measurement = [ & ] ( int run_index ) {
sol::function r = lua[ "r" ];
int value = r.call<int>( );
return value;
};
meter.measure( measurement );
}
};
struct sol_protected_function_result_lua_bench {
void operator () ( nonius::chronometer meter ) const {
sol::state lua = prepare_lua_function_state( );
auto measurement = [ & ] ( int run_index ) {
sol::protected_function r = lua[ "r" ];
int value = r( );
return value;
};
meter.measure( measurement );
}
};
struct sol_protected_call_lua_bench {
void operator () ( nonius::chronometer meter ) const {
sol::state lua = prepare_lua_function_state( );
auto measurement = [ & ] ( int run_index ) {
sol::protected_function r = lua[ "r" ];
int value = r.call<int>( );
return value;
};
meter.measure( measurement );
}
};
struct c_direct_lua_bench {
void operator () ( nonius::chronometer meter ) const {
sol::state lua = prepare_lua_function_state( );
lua_State* L = lua.lua_state( );
auto measurement = [ & ] ( int run_index ) {
lua_getglobal( L, "r" );
lua_call( L, 0, 1 );
int value = (int)lua_tonumber( L, -1 );
lua_pop( L, 1 );
return value;
};
meter.measure( measurement );
}
};
void bench_lua_function( const std::string& dir, std::string& configurationname, const std::string& platformname ) {
nonius::configuration cfg;
cfg.output_file = dir + "sol.functions (lua source) - " + configurationname + " " + platformname + ".html";
cfg.title = "sol::function (lua source) (" + configurationname + " " + platformname + ")";
cfg.samples = 100;
nonius::benchmark benchmarks[] = {
nonius::benchmark("function - function_result", sol_function_result_lua_bench()),
nonius::benchmark("function - call<>", sol_call_lua_bench()),
nonius::benchmark("protected_function - function_result", sol_protected_function_result_lua_bench()),
nonius::benchmark("protected_function - call<>", sol_protected_call_lua_bench()),
nonius::benchmark( "plain C", c_direct_lua_bench( ) ),
};
nonius::go( cfg, std::begin( benchmarks ), std::end( benchmarks ), nonius::html_reporter( ) );
}