mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
improved benchmarking
This commit is contained in:
parent
60b91b4c83
commit
8c9f8c3341
|
@ -2,6 +2,7 @@
|
||||||
#include "bench_lua_function.hpp"
|
#include "bench_lua_function.hpp"
|
||||||
#include "bench_get.hpp"
|
#include "bench_get.hpp"
|
||||||
#include "bench_set.hpp"
|
#include "bench_set.hpp"
|
||||||
|
#include "bench_usertype.hpp"
|
||||||
#include "nonius/nonius.h++"
|
#include "nonius/nonius.h++"
|
||||||
|
|
||||||
int main( int argc, char* argv[] ) {
|
int main( int argc, char* argv[] ) {
|
||||||
|
@ -12,4 +13,5 @@ int main( int argc, char* argv[] ) {
|
||||||
bench_cpp_function( "bench/", configurationname, platformname );
|
bench_cpp_function( "bench/", configurationname, platformname );
|
||||||
bench_get( "bench/", configurationname, platformname );
|
bench_get( "bench/", configurationname, platformname );
|
||||||
bench_set( "bench/", configurationname, platformname );
|
bench_set( "bench/", configurationname, platformname );
|
||||||
|
bench_usertype( "bench/", configurationname, platformname );
|
||||||
}
|
}
|
|
@ -1 +0,0 @@
|
||||||
#pragma once
|
|
105
bench_usertype.hpp
Normal file
105
bench_usertype.hpp
Normal file
|
@ -0,0 +1,105 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "sol.hpp"
|
||||||
|
#include "nonius/nonius.h++"
|
||||||
|
|
||||||
|
struct woof {
|
||||||
|
int var;
|
||||||
|
|
||||||
|
int func( int x ) {
|
||||||
|
return var + x;
|
||||||
|
}
|
||||||
|
|
||||||
|
int operator + (int x) const {
|
||||||
|
return var + x;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
static sol::state prepare_usertype_state( ) {
|
||||||
|
sol::state lua;
|
||||||
|
lua.new_usertype<woof>( "woof",
|
||||||
|
"var", &woof::var,
|
||||||
|
"func", &woof::func,
|
||||||
|
sol::meta_function::addition, &woof::operator+ );
|
||||||
|
lua.script( R"(
|
||||||
|
r = woof:new()
|
||||||
|
)" );
|
||||||
|
return lua;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct sol_usertype_special_bench {
|
||||||
|
void operator () ( nonius::chronometer meter ) const {
|
||||||
|
sol::state lua = prepare_usertype_state( );
|
||||||
|
auto measurement = [ & ] ( int run_index ) {
|
||||||
|
lua.script("x = r + 2");
|
||||||
|
};
|
||||||
|
meter.measure( measurement );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct sol_usertype_regular_bench {
|
||||||
|
void operator () ( nonius::chronometer meter ) const {
|
||||||
|
sol::state lua = prepare_usertype_state( );
|
||||||
|
auto measurement = [ & ] ( int run_index ) {
|
||||||
|
lua.script("x = r:func( 2 )");
|
||||||
|
};
|
||||||
|
meter.measure( measurement );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct sol_usertype_variable_bench {
|
||||||
|
void operator () ( nonius::chronometer meter ) const {
|
||||||
|
sol::state lua = prepare_usertype_state( );
|
||||||
|
auto measurement = [ & ] ( int run_index ) {
|
||||||
|
lua.script("x = r.var + 2");
|
||||||
|
};
|
||||||
|
meter.measure( measurement );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct c_usertype_special_bench {
|
||||||
|
void operator () ( nonius::chronometer meter ) const {
|
||||||
|
sol::state lua = prepare_usertype_state( );
|
||||||
|
lua_State* L = lua.lua_state( );
|
||||||
|
auto measurement = [ & ] ( int run_index ) {
|
||||||
|
|
||||||
|
};
|
||||||
|
meter.measure( measurement );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct c_usertype_regular_bench {
|
||||||
|
void operator () ( nonius::chronometer meter ) const {
|
||||||
|
sol::state lua = prepare_usertype_state( );
|
||||||
|
lua_State* L = lua.lua_state( );
|
||||||
|
auto measurement = [ & ] ( int run_index ) {
|
||||||
|
};
|
||||||
|
meter.measure( measurement );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct c_usertype_variable_bench {
|
||||||
|
void operator () ( nonius::chronometer meter ) const {
|
||||||
|
sol::state lua = prepare_usertype_state( );
|
||||||
|
lua_State* L = lua.lua_state( );
|
||||||
|
auto measurement = [ & ] ( int run_index ) {
|
||||||
|
};
|
||||||
|
meter.measure( measurement );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
void bench_usertype( const std::string& dir, std::string& configurationname, const std::string& platformname ) {
|
||||||
|
nonius::configuration cfg;
|
||||||
|
cfg.output_file = dir + "sol.usertype - " + configurationname + " " + platformname + ".html";
|
||||||
|
cfg.title = "sol usertype (" + configurationname + " " + platformname + ")";
|
||||||
|
cfg.samples = 100;
|
||||||
|
nonius::benchmark benchmarks [] = {
|
||||||
|
nonius::benchmark( "sol - usertype special", sol_usertype_special_bench( ) ),
|
||||||
|
nonius::benchmark( "sol - usertype regular", sol_usertype_regular_bench( ) ),
|
||||||
|
nonius::benchmark( "sol - usertype variable", sol_usertype_variable_bench( ) ),
|
||||||
|
//nonius::benchmark( "plain C - usertype special", c_usertype_special_bench( ) ),
|
||||||
|
//nonius::benchmark( "plain C - usertype regular", c_usertype_regular_bench( ) ),
|
||||||
|
//nonius::benchmark( "plain C - usertype variable", c_usertype_variable_bench( ) ),
|
||||||
|
};
|
||||||
|
nonius::go( cfg, std::begin( benchmarks ), std::end( benchmarks ), nonius::html_reporter( ) );
|
||||||
|
}
|
|
@ -63,7 +63,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
T get() const {
|
decltype(auto) get() const {
|
||||||
return tbl.template get<T>( key );
|
return tbl.template get<T>( key );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,19 +71,19 @@ public:
|
||||||
return get<std::string>();
|
return get<std::string>();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T, EnableIf<Not<std::is_same<Unqualified<T>, const char*>>, Not<std::is_same<Unqualified<T>, char>>, Not<std::is_same<Unqualified<T>, std::string>>, Not<std::is_same<Unqualified<T>, std::initializer_list<char>>>> = 0>
|
template<typename T, EnableIf<Not<is_string_constructible<T>>, is_lua_primitive<T>> = 0>
|
||||||
operator T () const {
|
operator T ( ) const {
|
||||||
return get<T>();
|
return get<T>( );
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, EnableIf<Not<is_string_constructible<T>>, Not<is_lua_primitive<T>>> = 0>
|
||||||
|
operator T& ( ) const {
|
||||||
|
return get<T&>( );
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename... Ret, typename... Args>
|
template<typename... Ret, typename... Args>
|
||||||
stack::get_return_or<function_result, Ret...> call(Args&&... args) {
|
decltype(auto) call(Args&&... args) {
|
||||||
return tbl.template get<function>(key)(types<Ret...>(), std::forward<Args>(args)...);
|
return get<function>().call<Ret...>(std::forward<Args>(args)...);
|
||||||
}
|
|
||||||
|
|
||||||
template<typename... Args>
|
|
||||||
function_result operator()(Args&&... args) {
|
|
||||||
return tbl.template get<function>(key)(std::forward<Args>(args)...);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -87,7 +87,7 @@ template<typename T, typename... Args>
|
||||||
struct And<T, Args...> : If<T, And<Args...>, Bool<false>> {};
|
struct And<T, Args...> : If<T, And<Args...>, Bool<false>> {};
|
||||||
|
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
struct Or : Bool<true> {};
|
struct Or : Bool<false> {};
|
||||||
|
|
||||||
template<typename T, typename... Args>
|
template<typename T, typename... Args>
|
||||||
struct Or<T, Args...> : If<T, Bool<true>, Or<Args...>> {};
|
struct Or<T, Args...> : If<T, Bool<true>, Or<Args...>> {};
|
||||||
|
@ -311,6 +311,9 @@ struct has_key_value_pair_impl {
|
||||||
template<typename T>
|
template<typename T>
|
||||||
struct has_key_value_pair : decltype(has_key_value_pair_impl::test<T>(0)) {};
|
struct has_key_value_pair : decltype(has_key_value_pair_impl::test<T>(0)) {};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
using is_string_constructible = Or<std::is_same<Unqualified<T>, const char*>, std::is_same<Unqualified<T>, char>, std::is_same<Unqualified<T>, std::string>, std::is_same<Unqualified<T>, std::initializer_list<char>>>;
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
auto unwrapper(T&& item) -> decltype(std::forward<T>(item)) {
|
auto unwrapper(T&& item) -> decltype(std::forward<T>(item)) {
|
||||||
return std::forward<T>(item);
|
return std::forward<T>(item);
|
||||||
|
|
|
@ -32,9 +32,6 @@ const nil_t nil {};
|
||||||
inline bool operator==(nil_t, nil_t) { return true; }
|
inline bool operator==(nil_t, nil_t) { return true; }
|
||||||
inline bool operator!=(nil_t, nil_t) { return false; }
|
inline bool operator!=(nil_t, nil_t) { return false; }
|
||||||
|
|
||||||
struct void_type : types<void> {}; // This is important because it allows myobject.call( Void, ... ) to work
|
|
||||||
const void_type Void {};
|
|
||||||
|
|
||||||
template<typename... T>
|
template<typename... T>
|
||||||
struct function_sig_t {};
|
struct function_sig_t {};
|
||||||
using function_t = function_sig_t<>;
|
using function_t = function_sig_t<>;
|
||||||
|
@ -146,10 +143,13 @@ template <>
|
||||||
struct lua_type_of<bool> : std::integral_constant<type, type::boolean> {};
|
struct lua_type_of<bool> : std::integral_constant<type, type::boolean> {};
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
struct lua_type_of<nil_t> : std::integral_constant<type, type::nil> {};
|
struct lua_type_of<nil_t> : std::integral_constant<type, type::nil> { };
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
struct lua_type_of<table> : std::integral_constant<type, type::table> {};
|
struct lua_type_of<table> : std::integral_constant<type, type::table> { };
|
||||||
|
|
||||||
|
template <>
|
||||||
|
struct lua_type_of<global_table> : std::integral_constant<type, type::table> { };
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
struct lua_type_of<object> : std::integral_constant<type, type::poly> {};
|
struct lua_type_of<object> : std::integral_constant<type, type::poly> {};
|
||||||
|
@ -160,6 +160,9 @@ struct lua_type_of<light_userdata> : std::integral_constant<type, type::lightuse
|
||||||
template <>
|
template <>
|
||||||
struct lua_type_of<function> : std::integral_constant<type, type::function> {};
|
struct lua_type_of<function> : std::integral_constant<type, type::function> {};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
struct lua_type_of<fast_function> : std::integral_constant<type, type::function> {};
|
||||||
|
|
||||||
template <typename Signature>
|
template <typename Signature>
|
||||||
struct lua_type_of<std::function<Signature>> : std::integral_constant<type, type::function>{};
|
struct lua_type_of<std::function<Signature>> : std::integral_constant<type, type::function>{};
|
||||||
|
|
||||||
|
@ -185,6 +188,9 @@ struct lua_type_of<T, typename std::enable_if<std::is_enum<T>::value>::type> : s
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct is_lua_primitive : std::integral_constant<bool, type::userdata != lua_type_of<Unqualified<T>>::value> { };
|
||||||
|
|
||||||
} // sol
|
} // sol
|
||||||
|
|
||||||
#endif // SOL_TYPES_HPP
|
#endif // SOL_TYPES_HPP
|
||||||
|
|
Loading…
Reference in New Issue
Block a user