root/examples/unordered_tuple_performance.cpp

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. BOOST_AUTO_TEST_SUITE
  2. BOOST_AUTO_TEST_CASE

   1 /******************************************************************************
   2 ** $Header: svn+ssh://jmmcg@svn.code.sf.net/p/libjmmcg/code/trunk/libjmmcg/examples/unordered_tuple_performance.cpp 2284 2018-05-23 11:02:06Z jmmcg $
   3 **
   4 ** Copyright (c) 2017 by J.M.McGuiness, coder@hussar.me.uk
   5 **
   6 ** This library is free software; you can redistribute it and/or
   7 ** modify it under the terms of the GNU Lesser General Public
   8 ** License as published by the Free Software Foundation; either
   9 ** version 2.1 of the License, or (at your option) any later version.
  10 **
  11 ** This library is distributed in the hope that it will be useful,
  12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14 ** Lesser General Public License for more details.
  15 **
  16 ** You should have received a copy of the GNU Lesser General Public
  17 ** License along with this library; if not, write to the Free Software
  18 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19 */
  20 
  21 #include "stdafx.h"
  22 
  23 #include "core/stats_output.hpp"
  24 
  25 #define BOOST_TEST_MODULE libjmmcg_tests
  26 #include <boost/test/included/unit_test.hpp>
  27 
  28 #include "core/ave_deviation_meter.hpp"
  29 #include "core/unordered_tuple.hpp"
  30 
  31 #include <chrono>
  32 #include <memory>
  33 #include <unordered_map>
  34 
  35 using timed_results_t=jmmcg::ave_deviation_meter<unsigned long long>;
  36 
  37 struct base {
  38         using key_type=std::int32_t;
  39 
  40         struct hasher {
  41                 constexpr key_type operator()(key_type k) const noexcept(true) {
  42                         return k;
  43                 }
  44                 constexpr key_type operator()(key_type k, std::size_t) const noexcept(true) {
  45                         return k;
  46                 }
  47         };
  48 
  49         virtual ~base()=default;
  50 
  51         virtual unsigned long fn(unsigned long j) const=0;
  52 };
  53 struct derived1 final : base {
  54         static constexpr base::key_type hash=0;
  55 
  56         const unsigned long i_;
  57 
  58         explicit derived1(unsigned long i)
  59         : i_(i) {}
  60 
  61         unsigned long fn(unsigned long j) const override {
  62                 return i_*j;
  63         }
  64 };
  65 constexpr base::key_type derived1::hash;
  66 struct derived2 final : base {
  67         static constexpr base::key_type hash=1;
  68 
  69         const unsigned long i_;
  70 
  71         explicit derived2(unsigned long i)
  72         : i_(i) {}
  73 
  74         unsigned long fn(unsigned long j) const override {
  75                 return i_+j;
  76         }
  77 };
  78 constexpr base::key_type derived2::hash;
  79 
  80 template<class T>
  81 struct extract {
  82         static constexpr const typename T::key_type value=T::hash;
  83 };
  84 
  85 BOOST_AUTO_TEST_SUITE(unordered_tuple_tests)
  86 
  87 BOOST_AUTO_TEST_SUITE(performance, *boost::unit_test::fixture<jmmcg::stats_to_csv::wrapper>(std::string("unordered_tuple_performance.csv")))
  88 
  89 /**
  90         \test <a href="./examples/unordered_tuple_performance.svg">Graph</a> of performance results for operations on an unordered_tuple & unordered_map.
  91                         ==========================================================================================
  92         Call a pure-virtual member-function in a base class of a class found in an unordered collection.
  93 */
  94 BOOST_AUTO_TEST_CASE(unordered_tuple) {
  95         using collection_type=jmmcg::unordered_tuple<base::key_type, base, base::hasher, extract, derived1, derived2>;
  96 
  97         collection_type colln(derived1(667), derived2(42));
  98         unsigned long res=0;
  99 #ifdef JMMCG_PERFORMANCE_TESTS
 100         const unsigned long test_size=2<<22;
 101 #else
 102         const unsigned long test_size=2<<2;
 103 #endif
 104         const unsigned short loops_for_conv=1000;
 105         const double perc_conv_estimate=2.0;
 106 
 107         const std::pair<timed_results_t, bool> timed_results(jmmcg::compute_average_deviation<timed_results_t::value_type>(
 108                 perc_conv_estimate,
 109                 loops_for_conv,
 110                 [&colln, &res]() {
 111                         const auto t1=std::chrono::high_resolution_clock::now();
 112                         for (unsigned long i=0; i<test_size; ++i) {
 113                                 res+=colln[i%2].fn(i);
 114                         }
 115                         const auto t2=std::chrono::high_resolution_clock::now();
 116                         return timed_results_t::value_type(static_cast<double>(test_size)*1000000/std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count());
 117                 }
 118         ));
 119         BOOST_CHECK_GT(res, 0);
 120         std::cout<<typeid(collection_type).name()<<" operations/sec="<<timed_results.first<<std::endl;
 121 #ifdef JMMCG_PERFORMANCE_TESTS
 122         jmmcg::stats_to_csv::handle->stats<<timed_results.first.to_csv()<<std::flush;
 123         BOOST_CHECK(!timed_results.second);
 124 #endif
 125 }
 126 
 127 /**
 128         \test <a href="./examples/unordered_tuple_performance.svg">Graph</a> of performance results for operations on an unordered_map.
 129                         ==========================================================================================
 130         Call a pure-virtual member-function in a base class of a class found in a std::unordered_map.
 131 */
 132 BOOST_AUTO_TEST_CASE(unordered_map) {
 133         using base_t=std::shared_ptr<base>;
 134         using collection_type=std::unordered_map<base::key_type, base_t>;
 135 
 136         collection_type colln;
 137         colln.emplace(derived1::hash, std::make_shared<derived1>(667));
 138         colln.emplace(derived2::hash, std::make_shared<derived2>(42));
 139         unsigned long res=0;
 140 #ifdef JMMCG_PERFORMANCE_TESTS
 141         const unsigned long test_size=2<<22;
 142 #else
 143         const unsigned long test_size=2<<2;
 144 #endif
 145         const unsigned short loops_for_conv=1000;
 146         const double perc_conv_estimate=2.0;
 147 
 148         const std::pair<timed_results_t, bool> timed_results(jmmcg::compute_average_deviation<timed_results_t::value_type>(
 149                 perc_conv_estimate,
 150                 loops_for_conv,
 151                 [&colln, &res]() {
 152                         const auto t1=std::chrono::high_resolution_clock::now();
 153                         for (unsigned long i=0; i<test_size; ++i) {
 154                                 res+=colln[i%2]->fn(i);
 155                         }
 156                         const auto t2=std::chrono::high_resolution_clock::now();
 157                         return timed_results_t::value_type(static_cast<double>(test_size)*1000000/std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count());
 158                 }
 159         ));
 160         BOOST_CHECK_GT(res, 0);
 161         std::cout<<typeid(collection_type).name()<<" operations/sec="<<timed_results.first<<std::endl;
 162 #ifdef JMMCG_PERFORMANCE_TESTS
 163         jmmcg::stats_to_csv::handle->stats<<timed_results.first.to_csv()<<std::flush;
 164         BOOST_CHECK(!timed_results.second);
 165 #endif
 166 }
 167 
 168 BOOST_AUTO_TEST_SUITE_END()
 169 
 170 BOOST_AUTO_TEST_SUITE_END()

/* [<][>][^][v][top][bottom][index][help] */