Implement variable size MyFatInt

This commit is contained in:
Ion Gaztañaga
2026-07-30 01:31:36 +02:00
parent b506adfa88
commit 3c8ea73b3c
2 changed files with 34 additions and 54 deletions
+29 -50
View File
@@ -173,78 +173,57 @@ class MyInt
friend bool operator>=(const MyInt& a, const MyInt& b) { return a.int_ >= b.int_; }
};
//MyFatInt: same idea as MyInt but storing Count ints (32 bytes by default) so
//benchmarks can also measure bigger elements, whose special member functions do
//proportionally more work. Only the first int participates in int_value() and
//in the comparison operators; the remaining ones are payload that the copy and
//destroy operations must still touch.
template<std::size_t Count = 8>
class MyFatInt
{
int int0_;
int int1_;
int int2_;
int int3_;
int int4_;
int int5_;
int int6_;
int int7_;
int ints_[Count];
public:
inline explicit MyFatInt(int i = 0)
: int0_(i++)
, int1_(i++)
, int2_(i++)
, int3_(i++)
, int4_(i++)
, int5_(i++)
, int6_(i++)
, int7_(i++)
{}
{
for(std::size_t n = 0; n != Count; ++n)
ints_[n] = i++;
}
inline MyFatInt(const MyFatInt &other)
: int0_(other.int0_)
, int1_(other.int1_)
, int2_(other.int2_)
, int3_(other.int3_)
, int4_(other.int4_)
, int5_(other.int5_)
, int6_(other.int6_)
, int7_(other.int7_)
{}
{
for(std::size_t n = 0; n != Count; ++n)
ints_[n] = other.ints_[n];
}
inline MyFatInt & operator=(const MyFatInt &other)
{
int0_ = other.int0_;
int1_ = other.int1_;
int2_ = other.int2_;
int3_ = other.int3_;
int4_ = other.int4_;
int5_ = other.int5_;
int6_ = other.int6_;
int7_ = other.int7_;
for(std::size_t n = 0; n != Count; ++n)
ints_[n] = other.ints_[n];
return *this;
}
inline ~MyFatInt()
{
int0_ = 0;
int1_ = 0;
int2_ = 0;
int3_ = 0;
int4_ = 0;
int5_ = 0;
int6_ = 0;
int7_ = 0;
for(std::size_t n = 0; n != Count; ++n)
ints_[n] = 0;
}
inline int int_value() const { return int0_; }
inline int int_value() const { return ints_[0]; }
friend inline bool operator==(const MyFatInt& a, const MyFatInt& b) { return a.int0_ == b.int0_; }
friend inline bool operator!=(const MyFatInt& a, const MyFatInt& b) { return a.int0_ != b.int0_; }
friend inline bool operator<(const MyFatInt& a, const MyFatInt& b) { return a.int0_ < b.int0_; }
friend inline bool operator>(const MyFatInt& a, const MyFatInt& b) { return a.int0_ > b.int0_; }
friend inline bool operator<=(const MyFatInt& a, const MyFatInt& b) { return a.int0_ <= b.int0_; }
friend inline bool operator>=(const MyFatInt& a, const MyFatInt& b) { return a.int0_ >= b.int0_; }
friend inline bool operator==(const MyFatInt& a, const MyFatInt& b) { return a.ints_[0] == b.ints_[0]; }
friend inline bool operator!=(const MyFatInt& a, const MyFatInt& b) { return a.ints_[0] != b.ints_[0]; }
friend inline bool operator<(const MyFatInt& a, const MyFatInt& b) { return a.ints_[0] < b.ints_[0]; }
friend inline bool operator>(const MyFatInt& a, const MyFatInt& b) { return a.ints_[0] > b.ints_[0]; }
friend inline bool operator<=(const MyFatInt& a, const MyFatInt& b) { return a.ints_[0] <= b.ints_[0]; }
friend inline bool operator>=(const MyFatInt& a, const MyFatInt& b) { return a.ints_[0] >= b.ints_[0]; }
};
inline int int_value(int x) { return x; }
inline int int_value(const MyInt& x) { return x.int_value(); }
inline int int_value(const MyFatInt& x) { return x.int_value(); }
template<std::size_t Count>
inline int int_value(const MyFatInt<Count>& x) { return x.int_value(); }
//Benchmark element. The NonTrivial boolean (chosen by the caller, true by
//default) selects between two layouts of identical size:
+4 -3
View File
@@ -84,7 +84,7 @@
#include "../bench/bench_utils.hpp"
#ifndef BOOST_CONTAINER_BENCH_SEGMENTED_GROUP
//#define BOOST_CONTAINER_BENCH_SEGMENTED_GROUP 15
#define BOOST_CONTAINER_BENCH_SEGMENTED_GROUP 20
#endif //BOOST_CONTAINER_BENCH_SEGMENTED_GROUP
namespace bc = boost::container;
@@ -654,6 +654,7 @@ inline void print_group_header(int group, const char* desc, const char* vtname)
{
g_group_geomean.reset();
std::cout << "\n===== Group " << group << ": " << desc << " [" << vtname << "] =====\n";
print_subheader();
}
inline void print_group_geomean()
@@ -2112,7 +2113,6 @@ void run_all(const C& c, std::size_t iters, const char* cname)
vec_t cv(c.begin(), c.end());
g_geomean.reset();
print_subheader();
//////////////////////////////////////////////////////////////////
// Group 10: single range, sequential access
@@ -2554,6 +2554,7 @@ int main()
{
//run_benchmarks<int>();
run_benchmarks<MyInt>();
run_benchmarks<MyFatInt>();
run_benchmarks<MyFatInt<4> >();
run_benchmarks<MyFatInt<8> >();
return 0;
}