From 8f55000f6fecf6b5182c00e0dbc201c2bef3a2fc Mon Sep 17 00:00:00 2001 From: Beman Date: Thu, 23 May 2013 10:11:29 -0400 Subject: [PATCH] Finally getting useful speed_test results from both GCC and VC++ --- build/Jamfile.v2 | 2 +- test/msvc2012/speed_test/speed_test.vcxproj | 5 +- test/speed_test.cpp | 148 +++++--------------- test/speed_test_functions.cpp | 36 +++++ test/speed_test_functions.hpp | 44 ++++++ 5 files changed, 118 insertions(+), 117 deletions(-) create mode 100644 test/speed_test_functions.cpp create mode 100644 test/speed_test_functions.hpp diff --git a/build/Jamfile.v2 b/build/Jamfile.v2 index b70ecf1..fe80c78 100644 --- a/build/Jamfile.v2 +++ b/build/Jamfile.v2 @@ -10,7 +10,7 @@ project # msvc:on ; -SOURCES = speed_test ; +SOURCES = speed_test speed_test_functions ; exe "speed_test" diff --git a/test/msvc2012/speed_test/speed_test.vcxproj b/test/msvc2012/speed_test/speed_test.vcxproj index 46d4f83..181b816 100644 --- a/test/msvc2012/speed_test/speed_test.vcxproj +++ b/test/msvc2012/speed_test/speed_test.vcxproj @@ -60,7 +60,7 @@ true - "$(TargetDir)\$(TargetName).exe" 1 + "$(TargetDir)\$(TargetName).exe" 1000000 @@ -80,11 +80,12 @@ true - "$(TargetDir)\$(TargetName).exe" 10000000000 + "$(TargetDir)\$(TargetName).exe" 1000000000 + diff --git a/test/speed_test.cpp b/test/speed_test.cpp index baecb46..dcae5a2 100644 --- a/test/speed_test.cpp +++ b/test/speed_test.cpp @@ -12,6 +12,7 @@ #include +#include "speed_test_functions.hpp" #include #include #include @@ -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 - result_type test_add_T_to_T(T x) + template + 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(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 - 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(y); - boost::timer::cpu_times times = t.elapsed(); - result.cpu_time = (times.system + times.user); - t.report(); - return result; + cout << " no +\n "; + time(user::return_x_big_int32); + cout << " + int32_t argument\n "; + time(user::return_x_plus_y_big_int32); + cout << " + int32_t by value\n "; + time(user::return_x_plus_y_value_big_int32); + cout << " + int32_t in place\n "; + time(user::return_x_plus_y_in_place_big_int32); + cout << " + big_int32_t\n "; + time(user::return_x_plus_z_big_int32); } - template - 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(y); - boost::timer::cpu_times times = t.elapsed(); - result.cpu_time = (times.system + times.user); - t.report(); - return result; - } - - template - 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(y); - boost::timer::cpu_times times = t.elapsed(); - result.cpu_time = (times.system + times.user); - t.report(); - return result; - } - - template - void test(T x) - { - test_add_T_to_T(x); - if (Order == order::big) - test_add_conditionally_reversed_T_to_T(x, big_tag()); - else - test_add_conditionally_reversed_T_to_T(x, little_tag()); - test_add_Endian_to_T(EndianT(x)); + cout << " no +\n "; + time(user::return_x_little_int32); + cout << " + int32_t argument\n "; + time(user::return_x_plus_y_little_int32); + cout << " + int32_t by value\n "; + time(user::return_x_plus_y_value_little_int32); + cout << " + int32_t in place\n "; + time(user::return_x_plus_y_in_place_little_int32); + cout << " + little_int32_t\n "; + time(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(0x1122); - cout << endl << "int16_t, big_int16_t" << endl; - test(0x1122); - - cout << endl << "int16_t, little_16_t" << endl; - test(0x1122); - cout << endl << "int16_t, little_int16_t" << endl; - test(0x1122); - - cout << endl << "------------------------------------------------------" << endl; - - cout << endl << "int32_t, big_32_t" << endl; - test(0x11223344); - cout << endl << "int32_t, big_int32_t" << endl; - test(0x11223344); - - cout << endl << "int32_t, little_32_t" << endl; - test(0x11223344); - cout << endl << "int32_t, little_int32_t" << endl; - test(0x11223344); - - cout << endl << "------------------------------------------------------" << endl; - - cout << endl << "int64_t, big_64_t" << endl; - test(0x1122334455667788); - cout << endl << "int64_t, big_int64_t" << endl; - test(0x1122334455667788); - - cout << endl << "int64_t, little_64_t" << endl; - test(0x1122334455667788); - cout << endl << "int64_t, little_int64_t" << endl; - test(0x1122334455667788); + cout << endl << "big, 32-bit..." << endl; + test_big_int32(); + cout << endl << "little, 32-bit..." << endl; + test_little_int32(); cout << endl << "------------------------------------------------------" << endl; diff --git a/test/speed_test_functions.cpp b/test/speed_test_functions.cpp new file mode 100644 index 0000000..e974aff --- /dev/null +++ b/test/speed_test_functions.cpp @@ -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;} + +} diff --git a/test/speed_test_functions.hpp b/test/speed_test_functions.hpp new file mode 100644 index 0000000..9d4b473 --- /dev/null +++ b/test/speed_test_functions.hpp @@ -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 +#include + +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