forked from boostorg/variant2
Add benchmark1.cpp
This commit is contained in:
161
benchmark/benchmark1.cpp
Normal file
161
benchmark/benchmark1.cpp
Normal file
@ -0,0 +1,161 @@
|
||||
#if defined(ONLY_V2)
|
||||
# define NO_BV
|
||||
# define NO_SV
|
||||
#endif
|
||||
|
||||
#if defined(ONLY_BV)
|
||||
# define NO_V2
|
||||
# define NO_SV
|
||||
#endif
|
||||
|
||||
#if defined(ONLY_SV)
|
||||
# define NO_V2
|
||||
# define NO_BV
|
||||
#endif
|
||||
|
||||
#if !defined(NO_V2)
|
||||
#include <boost/variant2/variant.hpp>
|
||||
#endif
|
||||
|
||||
#if !defined(NO_BV)
|
||||
#include <boost/variant.hpp>
|
||||
#endif
|
||||
|
||||
#if !defined(NO_SV)
|
||||
#include <variant>
|
||||
#endif
|
||||
|
||||
#include <type_traits>
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
|
||||
template<class T> struct is_numeric: std::integral_constant<bool, std::is_integral<T>::value || std::is_floating_point<T>::value>
|
||||
{
|
||||
};
|
||||
|
||||
template<class T, class U> struct have_addition: std::integral_constant<bool, is_numeric<T>::value && is_numeric<U>::value>
|
||||
{
|
||||
};
|
||||
|
||||
template<class T, class U, class E = std::enable_if_t<have_addition<T, U>::value>> auto add( T const& t, U const& u )
|
||||
{
|
||||
return t + u;
|
||||
}
|
||||
|
||||
template<class T, class U, class E = std::enable_if_t<!have_addition<T, U>::value>> double add( T const& /*t*/, U const& /*u*/ )
|
||||
{
|
||||
throw std::logic_error( "Invalid addition" );
|
||||
}
|
||||
|
||||
inline double to_double( double const& v )
|
||||
{
|
||||
return v;
|
||||
}
|
||||
|
||||
#if !defined(NO_V2)
|
||||
|
||||
template<class... T> boost::variant2::variant<T...> operator+( boost::variant2::variant<T...> const& v1, boost::variant2::variant<T...> const& v2 )
|
||||
{
|
||||
return visit( [&]( auto const& x1, auto const & x2 ) -> boost::variant2::variant<T...> { return add( x1, x2 ); }, v1, v2 );
|
||||
}
|
||||
|
||||
template<class... T> double to_double( boost::variant2::variant<T...> const& v )
|
||||
{
|
||||
return boost::variant2::get<double>( v );
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#if !defined(NO_BV)
|
||||
|
||||
template<class... T> boost::variant<T...> operator+( boost::variant<T...> const& v1, boost::variant<T...> const& v2 )
|
||||
{
|
||||
return boost::apply_visitor( [&]( auto const& x1, auto const & x2 ) -> boost::variant<T...> { return add( x1, x2 ); }, v1, v2 );
|
||||
}
|
||||
|
||||
template<class... T> double to_double( boost::variant<T...> const& v )
|
||||
{
|
||||
return boost::get<double>( v );
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#if !defined(NO_SV)
|
||||
|
||||
template<class... T> std::variant<T...> operator+( std::variant<T...> const& v1, std::variant<T...> const& v2 )
|
||||
{
|
||||
return visit( [&]( auto const& x1, auto const & x2 ) -> std::variant<T...> { return add( x1, x2 ); }, v1, v2 );
|
||||
}
|
||||
|
||||
template<class... T> double to_double( std::variant<T...> const& v )
|
||||
{
|
||||
return std::get<double>( v );
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
template<class V> void test_( long long N )
|
||||
{
|
||||
std::vector<V> w;
|
||||
// lack of reserve is deliberate
|
||||
|
||||
auto tp1 = std::chrono::high_resolution_clock::now();
|
||||
|
||||
for( long long i = 0; i < N; ++i )
|
||||
{
|
||||
V v;
|
||||
|
||||
if( i % 7 == 0 )
|
||||
{
|
||||
v = i / 7;
|
||||
}
|
||||
else
|
||||
{
|
||||
v = i / 7.0;
|
||||
}
|
||||
|
||||
w.push_back( v );
|
||||
}
|
||||
|
||||
V s = 0.0;
|
||||
|
||||
for( long long i = 0; i < N; ++i )
|
||||
{
|
||||
s = s + w[ i ];
|
||||
}
|
||||
|
||||
auto tp2 = std::chrono::high_resolution_clock::now();
|
||||
|
||||
std::cout << std::setw( 6 ) << std::chrono::duration_cast<std::chrono::milliseconds>( tp2 - tp1 ).count() << " ms; S=" << to_double( s ) << "\n";
|
||||
}
|
||||
|
||||
template<class... T> void test( long long N )
|
||||
{
|
||||
std::cout << "N=" << N << ":\n";
|
||||
|
||||
std::cout << " double: "; test_<double>( N );
|
||||
#if !defined(NO_V2)
|
||||
std::cout << " variant2: "; test_<boost::variant2::variant<T...>>( N );
|
||||
#endif
|
||||
#if !defined(NO_BV)
|
||||
std::cout << "boost::variant: "; test_<boost::variant<T...>>( N );
|
||||
#endif
|
||||
#if !defined(NO_SV)
|
||||
std::cout << " std::variant: "; test_<std::variant<T...>>( N );
|
||||
#endif
|
||||
|
||||
std::cout << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
long long const N = 100'000'000LL;
|
||||
|
||||
test<long long, double>( N );
|
||||
test<std::nullptr_t, long long, double, std::string, std::vector<std::string>, std::map<std::string, std::string>>( N );
|
||||
}
|
161
benchmark/benchmark1.md
Normal file
161
benchmark/benchmark1.md
Normal file
@ -0,0 +1,161 @@
|
||||
# benchmark1.cpp results
|
||||
|
||||
## VS 2017 15.9.7 64 bit (cl.exe 19.16, /std:c++17)
|
||||
|
||||
### /Od
|
||||
|
||||
#### Compile time
|
||||
|
||||
```
|
||||
variant2 (-DONLY_V2): 1837 ms
|
||||
boost::variant (-DONLY_BV): 2627 ms
|
||||
std::variant (-DONLY_SV): 1425 ms
|
||||
```
|
||||
|
||||
#### Run time
|
||||
|
||||
```
|
||||
N=100000000:
|
||||
double: 9041 ms; S=7.14286e+14
|
||||
variant2: 48367 ms; S=7.14286e+14
|
||||
boost::variant: 102776 ms; S=7.14286e+14
|
||||
std::variant: 40590 ms; S=7.14286e+14
|
||||
|
||||
N=100000000:
|
||||
double: 9029 ms; S=7.14286e+14
|
||||
variant2: 92962 ms; S=7.14286e+14
|
||||
boost::variant: 110441 ms; S=7.14286e+14
|
||||
std::variant: 92974 ms; S=7.14286e+14
|
||||
```
|
||||
|
||||
### /O2 /NDEBUG
|
||||
|
||||
#### Compile time
|
||||
|
||||
```
|
||||
variant2 (-DONLY_V2): 2571 ms
|
||||
boost::variant (-DONLY_BV): 3335 ms
|
||||
std::variant (-DONLY_SV): 1903 ms
|
||||
```
|
||||
|
||||
#### Run time
|
||||
|
||||
```
|
||||
N=100000000:
|
||||
double: 1949 ms; S=7.14286e+14
|
||||
variant2: 4176 ms; S=7.14286e+14
|
||||
boost::variant: 11312 ms; S=7.14286e+14
|
||||
std::variant: 4617 ms; S=7.14286e+14
|
||||
|
||||
N=100000000:
|
||||
double: 1949 ms; S=7.14286e+14
|
||||
variant2: 11807 ms; S=7.14286e+14
|
||||
boost::variant: 15632 ms; S=7.14286e+14
|
||||
std::variant: 10725 ms; S=7.14286e+14
|
||||
```
|
||||
|
||||
## g++ 7.4.0 -std=c++17 (Cygwin 64 bit)
|
||||
|
||||
### -O0
|
||||
|
||||
#### Compile time
|
||||
|
||||
```
|
||||
variant2 (-DONLY_V2): 2734 ms
|
||||
boost::variant (-DONLY_BV): 4308 ms
|
||||
std::variant (-DONLY_SV): 2298 ms
|
||||
```
|
||||
|
||||
#### Run time
|
||||
|
||||
```
|
||||
N=100000000:
|
||||
double: 3620 ms; S=7.14286e+14
|
||||
variant2: 29214 ms; S=7.14286e+14
|
||||
boost::variant: 88492 ms; S=7.14286e+14
|
||||
std::variant: 39510 ms; S=7.14286e+14
|
||||
|
||||
N=100000000:
|
||||
double: 3642 ms; S=7.14286e+14
|
||||
variant2: 75822 ms; S=7.14286e+14
|
||||
boost::variant: 96680 ms; S=7.14286e+14
|
||||
std::variant: 66411 ms; S=7.14286e+14
|
||||
```
|
||||
|
||||
### -O1
|
||||
|
||||
#### Compile time
|
||||
|
||||
```
|
||||
variant2 (-DONLY_V2): 2103 ms
|
||||
boost::variant (-DONLY_BV): 3398 ms
|
||||
std::variant (-DONLY_SV): 1841 ms
|
||||
```
|
||||
|
||||
#### Run time
|
||||
|
||||
```
|
||||
N=100000000:
|
||||
double: 1576 ms; S=7.14286e+14
|
||||
variant2: 3424 ms; S=7.14286e+14
|
||||
boost::variant: 4356 ms; S=7.14286e+14
|
||||
std::variant: 3764 ms; S=7.14286e+14
|
||||
|
||||
N=100000000:
|
||||
double: 1582 ms; S=7.14286e+14
|
||||
variant2: 9062 ms; S=7.14286e+14
|
||||
boost::variant: 9603 ms; S=7.14286e+14
|
||||
std::variant: 8825 ms; S=7.14286e+14
|
||||
```
|
||||
|
||||
### -O2 -DNDEBUG
|
||||
|
||||
#### Compile time
|
||||
|
||||
```
|
||||
variant2 (-DONLY_V2): 2276 ms
|
||||
boost::variant (-DONLY_BV): 3647 ms
|
||||
std::variant (-DONLY_SV): 2111 ms
|
||||
```
|
||||
|
||||
#### Run time
|
||||
|
||||
```
|
||||
N=100000000:
|
||||
double: 1643 ms; S=7.14286e+14
|
||||
variant2: 3070 ms; S=7.14286e+14
|
||||
boost::variant: 3385 ms; S=7.14286e+14
|
||||
std::variant: 3880 ms; S=7.14286e+14
|
||||
|
||||
N=100000000:
|
||||
double: 1622 ms; S=7.14286e+14
|
||||
variant2: 8101 ms; S=7.14286e+14
|
||||
boost::variant: 8611 ms; S=7.14286e+14
|
||||
std::variant: 8694 ms; S=7.14286e+14
|
||||
```
|
||||
|
||||
### -O3 -DNDEBUG
|
||||
|
||||
#### Compile time
|
||||
|
||||
```
|
||||
variant2 (-DONLY_V2): 2390 ms
|
||||
boost::variant (-DONLY_BV): 3768 ms
|
||||
std::variant (-DONLY_SV): 2094 ms
|
||||
```
|
||||
|
||||
#### Run time
|
||||
|
||||
```
|
||||
N=100000000:
|
||||
double: 1611 ms; S=7.14286e+14
|
||||
variant2: 2975 ms; S=7.14286e+14
|
||||
boost::variant: 3232 ms; S=7.14286e+14
|
||||
std::variant: 3726 ms; S=7.14286e+14
|
||||
|
||||
N=100000000:
|
||||
double: 1603 ms; S=7.14286e+14
|
||||
variant2: 8157 ms; S=7.14286e+14
|
||||
boost::variant: 8419 ms; S=7.14286e+14
|
||||
std::variant: 8659 ms; S=7.14286e+14
|
||||
```
|
Reference in New Issue
Block a user