mirror of
https://github.com/boostorg/endian.git
synced 2025-07-31 13:07:24 +02:00
Finally getting useful speed_test results from both GCC and VC++
This commit is contained in:
@@ -10,7 +10,7 @@ project
|
||||
# <toolset>msvc:<asynch-exceptions>on
|
||||
;
|
||||
|
||||
SOURCES = speed_test ;
|
||||
SOURCES = speed_test speed_test_functions ;
|
||||
|
||||
|
||||
exe "speed_test"
|
||||
|
@@ -60,7 +60,7 @@
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
<PostBuildEvent>
|
||||
<Command>"$(TargetDir)\$(TargetName).exe" 1</Command>
|
||||
<Command>"$(TargetDir)\$(TargetName).exe" 1000000</Command>
|
||||
</PostBuildEvent>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
@@ -80,11 +80,12 @@
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
</Link>
|
||||
<PostBuildEvent>
|
||||
<Command>"$(TargetDir)\$(TargetName).exe" 10000000000</Command>
|
||||
<Command>"$(TargetDir)\$(TargetName).exe" 1000000000</Command>
|
||||
</PostBuildEvent>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="..\..\speed_test.cpp" />
|
||||
<ClCompile Include="..\..\speed_test_functions.cpp" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
|
@@ -12,6 +12,7 @@
|
||||
|
||||
#include <boost/endian/detail/disable_warnings.hpp>
|
||||
|
||||
#include "speed_test_functions.hpp"
|
||||
#include <boost/endian/conversion.hpp>
|
||||
#include <boost/endian/types.hpp>
|
||||
#include <boost/cstdint.hpp>
|
||||
@@ -34,13 +35,6 @@ namespace
|
||||
int places = 3; // decimal places for times
|
||||
bool verbose (false);
|
||||
|
||||
struct result_type
|
||||
{
|
||||
nanosecond_t cpu_time; // system + user time
|
||||
uint64_t v; // value computed; returning this may prevent
|
||||
// optimizer from optimizing away the timing loop
|
||||
};
|
||||
|
||||
void process_command_line(int argc, char * argv[])
|
||||
{
|
||||
for (int a = 0; a < argc; ++a)
|
||||
@@ -75,7 +69,7 @@ namespace
|
||||
|
||||
if (argc < 2)
|
||||
{
|
||||
cout << "Usage: benchmark n [Options]\n"
|
||||
cout << "Usage: speed_test n [Options]\n"
|
||||
" The argument n specifies the number of test cases to run\n"
|
||||
" Options:\n"
|
||||
" -v Verbose messages\n"
|
||||
@@ -86,94 +80,49 @@ namespace
|
||||
|
||||
//--------------------------------------------------------------------------------------//
|
||||
|
||||
template <class T>
|
||||
result_type test_add_T_to_T(T x)
|
||||
template <class T, class EndianT, class Function>
|
||||
void time(Function f)
|
||||
{
|
||||
cout << "Add T to T ..." << endl;
|
||||
result_type result;
|
||||
T x(0);
|
||||
T y(0);
|
||||
EndianT z(0);
|
||||
boost::timer::auto_cpu_timer t(places);
|
||||
for (uint64_t i = 0; i < n; ++i)
|
||||
{
|
||||
y += x;
|
||||
f(x, y, z);
|
||||
}
|
||||
t.stop();
|
||||
result.v = static_cast<uint64_t>(y);
|
||||
boost::timer::cpu_times times = t.elapsed();
|
||||
result.cpu_time = (times.system + times.user);
|
||||
// result.cpu_time = (times.system + times.user);
|
||||
t.report();
|
||||
return result;
|
||||
}
|
||||
|
||||
struct big_tag {};
|
||||
struct little_tag {};
|
||||
|
||||
template <class T>
|
||||
result_type test_add_conditionally_reversed_T_to_T(T x, big_tag)
|
||||
void test_big_int32()
|
||||
{
|
||||
cout << "add_conditionally_reversed_T_to_T (big)..." << endl;
|
||||
result_type result;
|
||||
T y(0);
|
||||
boost::timer::auto_cpu_timer t(places);
|
||||
for (uint64_t i = 0; i < n; ++i)
|
||||
{
|
||||
y += ::boost::endian::big_endian_value(x);
|
||||
}
|
||||
t.stop();
|
||||
result.v = static_cast<uint64_t>(y);
|
||||
boost::timer::cpu_times times = t.elapsed();
|
||||
result.cpu_time = (times.system + times.user);
|
||||
t.report();
|
||||
return result;
|
||||
cout << " no +\n ";
|
||||
time<int32_t, big_int32_t>(user::return_x_big_int32);
|
||||
cout << " + int32_t argument\n ";
|
||||
time<int32_t, big_int32_t>(user::return_x_plus_y_big_int32);
|
||||
cout << " + int32_t by value\n ";
|
||||
time<int32_t, big_int32_t>(user::return_x_plus_y_value_big_int32);
|
||||
cout << " + int32_t in place\n ";
|
||||
time<int32_t, big_int32_t>(user::return_x_plus_y_in_place_big_int32);
|
||||
cout << " + big_int32_t\n ";
|
||||
time<int32_t, big_int32_t>(user::return_x_plus_z_big_int32);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
result_type test_add_conditionally_reversed_T_to_T(T x, little_tag)
|
||||
void test_little_int32()
|
||||
{
|
||||
cout << "add_conditionally_reversed_T_to_T (little)..." << endl;
|
||||
result_type result;
|
||||
T y(0);
|
||||
boost::timer::auto_cpu_timer t(places);
|
||||
for (uint64_t i = 0; i < n; ++i)
|
||||
{
|
||||
y += ::boost::endian::little_endian_value(x);
|
||||
}
|
||||
t.stop();
|
||||
result.v = static_cast<uint64_t>(y);
|
||||
boost::timer::cpu_times times = t.elapsed();
|
||||
result.cpu_time = (times.system + times.user);
|
||||
t.report();
|
||||
return result;
|
||||
}
|
||||
|
||||
template <class T, class EndianT>
|
||||
result_type test_add_Endian_to_T(EndianT x)
|
||||
{
|
||||
cout << "add_Endian_to_T..." << endl;
|
||||
result_type result;
|
||||
T y(0);
|
||||
boost::timer::auto_cpu_timer t(places);
|
||||
for (uint64_t i = 0; i < n; ++i)
|
||||
{
|
||||
y += x;
|
||||
}
|
||||
t.stop();
|
||||
result.v = static_cast<uint64_t>(y);
|
||||
boost::timer::cpu_times times = t.elapsed();
|
||||
result.cpu_time = (times.system + times.user);
|
||||
t.report();
|
||||
return result;
|
||||
}
|
||||
|
||||
template <class T, BOOST_SCOPED_ENUM(order) Order, class EndianT>
|
||||
void test(T x)
|
||||
{
|
||||
test_add_T_to_T<T>(x);
|
||||
if (Order == order::big)
|
||||
test_add_conditionally_reversed_T_to_T<T>(x, big_tag());
|
||||
else
|
||||
test_add_conditionally_reversed_T_to_T<T>(x, little_tag());
|
||||
test_add_Endian_to_T<T, EndianT>(EndianT(x));
|
||||
cout << " no +\n ";
|
||||
time<int32_t, little_int32_t>(user::return_x_little_int32);
|
||||
cout << " + int32_t argument\n ";
|
||||
time<int32_t, little_int32_t>(user::return_x_plus_y_little_int32);
|
||||
cout << " + int32_t by value\n ";
|
||||
time<int32_t, little_int32_t>(user::return_x_plus_y_value_little_int32);
|
||||
cout << " + int32_t in place\n ";
|
||||
time<int32_t, little_int32_t>(user::return_x_plus_y_in_place_little_int32);
|
||||
cout << " + little_int32_t\n ";
|
||||
time<int32_t, little_int32_t>(user::return_x_plus_z_little_int32);
|
||||
}
|
||||
|
||||
} // unnamed namespace
|
||||
@@ -188,39 +137,10 @@ int cpp_main(int argc, char* argv[])
|
||||
|
||||
cout << endl << "------------------------------------------------------" << endl;
|
||||
|
||||
cout << endl << "int16_t, big_16_t" << endl;
|
||||
test<int16_t, order::big, big_16_t>(0x1122);
|
||||
cout << endl << "int16_t, big_int16_t" << endl;
|
||||
test<int16_t, order::big, big_int16_t>(0x1122);
|
||||
|
||||
cout << endl << "int16_t, little_16_t" << endl;
|
||||
test<int16_t, order::little, little_16_t>(0x1122);
|
||||
cout << endl << "int16_t, little_int16_t" << endl;
|
||||
test<int16_t, order::little, little_int16_t>(0x1122);
|
||||
|
||||
cout << endl << "------------------------------------------------------" << endl;
|
||||
|
||||
cout << endl << "int32_t, big_32_t" << endl;
|
||||
test<int32_t, order::big, big_32_t>(0x11223344);
|
||||
cout << endl << "int32_t, big_int32_t" << endl;
|
||||
test<int32_t, order::big, big_int32_t>(0x11223344);
|
||||
|
||||
cout << endl << "int32_t, little_32_t" << endl;
|
||||
test<int32_t, order::little, little_32_t>(0x11223344);
|
||||
cout << endl << "int32_t, little_int32_t" << endl;
|
||||
test<int32_t, order::little, little_int32_t>(0x11223344);
|
||||
|
||||
cout << endl << "------------------------------------------------------" << endl;
|
||||
|
||||
cout << endl << "int64_t, big_64_t" << endl;
|
||||
test<int64_t, order::big, big_64_t>(0x1122334455667788);
|
||||
cout << endl << "int64_t, big_int64_t" << endl;
|
||||
test<int64_t, order::big, big_int64_t>(0x1122334455667788);
|
||||
|
||||
cout << endl << "int64_t, little_64_t" << endl;
|
||||
test<int64_t, order::little, little_64_t>(0x1122334455667788);
|
||||
cout << endl << "int64_t, little_int64_t" << endl;
|
||||
test<int64_t, order::little, little_int64_t>(0x1122334455667788);
|
||||
cout << endl << "big, 32-bit..." << endl;
|
||||
test_big_int32();
|
||||
cout << endl << "little, 32-bit..." << endl;
|
||||
test_little_int32();
|
||||
|
||||
cout << endl << "------------------------------------------------------" << endl;
|
||||
|
||||
|
36
test/speed_test_functions.cpp
Normal file
36
test/speed_test_functions.cpp
Normal file
@@ -0,0 +1,36 @@
|
||||
// speed_test_functions.cpp ----------------------------------------------------------//
|
||||
|
||||
// Copyright Beman Dawes 2013
|
||||
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// http://www.boost.org/LICENSE_1_0.txt
|
||||
|
||||
//--------------------------------------------------------------------------------------//
|
||||
|
||||
// These functions are in a separate compilation unit partially to defeat optimizers
|
||||
// and partially to create a worst case scenario. They are in a user namespace for
|
||||
// realism.
|
||||
|
||||
//--------------------------------------------------------------------------------------//
|
||||
|
||||
#include "speed_test_functions.hpp"
|
||||
|
||||
namespace user
|
||||
{
|
||||
|
||||
int32_t return_x_big_int32(int32_t x, int32_t, big_int32_t) BOOST_NOEXCEPT {return x;}
|
||||
int32_t return_x_little_int32(int32_t x, int32_t, little_int32_t) BOOST_NOEXCEPT {return x;}
|
||||
|
||||
int32_t return_x_plus_y_big_int32(int32_t x, int32_t y, big_int32_t) BOOST_NOEXCEPT {return x+y;}
|
||||
int32_t return_x_plus_y_little_int32(int32_t x, int32_t y, little_int32_t) BOOST_NOEXCEPT {return x+y;}
|
||||
|
||||
int32_t return_x_plus_y_value_big_int32(int32_t x, int32_t y, big_int32_t) BOOST_NOEXCEPT {return x+big_endian_value(y);}
|
||||
int32_t return_x_plus_y_value_little_int32(int32_t x, int32_t y, little_int32_t) BOOST_NOEXCEPT {return x+little_endian_value(y);}
|
||||
|
||||
int32_t return_x_plus_y_in_place_big_int32(int32_t x, int32_t y, big_int32_t) BOOST_NOEXCEPT {big_endian(y);return x+y;}
|
||||
int32_t return_x_plus_y_in_place_little_int32(int32_t x, int32_t y, little_int32_t) BOOST_NOEXCEPT {little_endian(y);return x+y;}
|
||||
|
||||
int32_t return_x_plus_z_big_int32(int32_t x, int32_t, big_int32_t z) BOOST_NOEXCEPT {return x+z;}
|
||||
int32_t return_x_plus_z_little_int32(int32_t x, int32_t, little_int32_t z) BOOST_NOEXCEPT {return x+z;}
|
||||
|
||||
}
|
44
test/speed_test_functions.hpp
Normal file
44
test/speed_test_functions.hpp
Normal file
@@ -0,0 +1,44 @@
|
||||
// speed_test_functions.hpp ----------------------------------------------------------//
|
||||
|
||||
// Copyright Beman Dawes 2013
|
||||
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// http://www.boost.org/LICENSE_1_0.txt
|
||||
|
||||
//--------------------------------------------------------------------------------------//
|
||||
|
||||
// These functions are separately compiled partially to defeat optimizers and
|
||||
// partially to create a worst case scenario. They are in a user namespace for
|
||||
// a bit of realism.
|
||||
|
||||
//--------------------------------------------------------------------------------------//
|
||||
|
||||
#ifndef BOOST_ENDIAN_SPEED_TEST_FUNCTIONS_HPP
|
||||
#define BOOST_ENDIAN_SPEED_TEST_FUNCTIONS_HPP
|
||||
|
||||
#include <boost/cstdint.hpp>
|
||||
#include <boost/endian/types.hpp>
|
||||
|
||||
namespace user
|
||||
{
|
||||
using namespace boost;
|
||||
using namespace boost::endian;
|
||||
|
||||
int32_t return_x_big_int32(int32_t x, int32_t y, big_int32_t z) BOOST_NOEXCEPT;
|
||||
int32_t return_x_little_int32(int32_t x, int32_t y, little_int32_t z) BOOST_NOEXCEPT;
|
||||
|
||||
int32_t return_x_plus_y_big_int32(int32_t x, int32_t y, big_int32_t z) BOOST_NOEXCEPT;
|
||||
int32_t return_x_plus_y_little_int32(int32_t x, int32_t y, little_int32_t z) BOOST_NOEXCEPT;
|
||||
|
||||
int32_t return_x_plus_y_value_big_int32(int32_t x, int32_t y, big_int32_t) BOOST_NOEXCEPT;
|
||||
int32_t return_x_plus_y_value_little_int32(int32_t x, int32_t y, little_int32_t z) BOOST_NOEXCEPT;
|
||||
|
||||
int32_t return_x_plus_y_in_place_big_int32(int32_t x, int32_t y, big_int32_t z) BOOST_NOEXCEPT;
|
||||
int32_t return_x_plus_y_in_place_little_int32(int32_t x, int32_t y, little_int32_t z) BOOST_NOEXCEPT;
|
||||
|
||||
int32_t return_x_plus_z_big_int32(int32_t x, int32_t y, big_int32_t z) BOOST_NOEXCEPT;
|
||||
int32_t return_x_plus_z_little_int32(int32_t x, int32_t y, little_int32_t z) BOOST_NOEXCEPT;
|
||||
|
||||
}
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user