| 1 |
#include <iostream> |
| 2 |
#include <vector> |
| 3 |
#include <iomanip> |
| 4 |
#include <stdint.h> |
| 5 |
#include <type_traits> |
| 6 |
#include <sys/time.h> |
| 7 |
#include <sys/resource.h> |
| 8 |
#include <gtest/gtest.h> |
| 9 |
|
| 10 |
struct TimingData |
| 11 |
{ |
| 12 |
double defaultInitTime; |
| 13 |
double pushBackInitTime; |
| 14 |
double copyTime; |
| 15 |
}; |
| 16 |
|
| 17 |
double |
| 18 |
cpu_time() |
| 19 |
{ |
| 20 |
struct rusage my_rusage; |
| 21 |
|
| 22 |
::getrusage(RUSAGE_SELF, &my_rusage); |
| 23 |
|
| 24 |
double seconds = my_rusage.ru_utime.tv_sec + my_rusage.ru_stime.tv_sec; |
| 25 |
double micro_seconds = my_rusage.ru_utime.tv_usec + my_rusage.ru_stime.tv_usec; |
| 26 |
|
| 27 |
return seconds + micro_seconds*1.0e-6; |
| 28 |
} |
| 29 |
|
| 30 |
struct PodThingy |
| 31 |
{ |
| 32 |
enum stuff_t { |
| 33 |
InvalidThingy = 0ull, |
| 34 |
MinThingy = 1ull, |
| 35 |
MaxThingy = ~0ull |
| 36 |
}; |
| 37 |
|
| 38 |
uint64_t m_value; |
| 39 |
|
| 40 |
PodThingy operator=(stuff_t val) { m_value = val; return *this; } |
| 41 |
}; |
| 42 |
|
| 43 |
struct NonPodThingy |
| 44 |
{ |
| 45 |
enum stuff_t { |
| 46 |
InvalidThingy = 0ull, |
| 47 |
MinThingy = 1ull, |
| 48 |
MaxThingy = ~0ull |
| 49 |
}; |
| 50 |
|
| 51 |
uint64_t m_value; |
| 52 |
|
| 53 |
NonPodThingy() : m_value(InvalidThingy) {}; |
| 54 |
NonPodThingy(uint64_t value) : m_value(value) {}; |
| 55 |
NonPodThingy operator=(stuff_t val) { m_value = val; return *this; } |
| 56 |
}; |
| 57 |
|
| 58 |
template<class THINGY> |
| 59 |
TimingData time_thingy(size_t num_thingies, size_t num_repeat) |
| 60 |
{ |
| 61 |
TimingData timingData; |
| 62 |
{ |
| 63 |
double time_start = cpu_time(); |
| 64 |
for (size_t i = 0; i < num_repeat; ++i) { |
| 65 |
std::vector<THINGY> thingies(num_thingies); |
| 66 |
} |
| 67 |
double time_stop = cpu_time(); |
| 68 |
|
| 69 |
timingData.defaultInitTime = time_stop - time_start; |
| 70 |
} |
| 71 |
|
| 72 |
{ |
| 73 |
double time_start = cpu_time(); |
| 74 |
for (size_t i = 0; i < num_repeat; ++i) { |
| 75 |
std::vector<THINGY> thingies; |
| 76 |
thingies.reserve(num_thingies); |
| 77 |
for (size_t j = 0; j < num_thingies; ++j) { |
| 78 |
thingies.push_back(THINGY()); |
| 79 |
} |
| 80 |
} |
| 81 |
double time_stop = cpu_time(); |
| 82 |
|
| 83 |
timingData.pushBackInitTime = time_stop - time_start; |
| 84 |
|
| 85 |
std::vector<THINGY> thingies; |
| 86 |
thingies.reserve(num_thingies); |
| 87 |
for (size_t i = 0; i < num_thingies; ++i) { |
| 88 |
thingies.push_back(THINGY()); |
| 89 |
} |
| 90 |
|
| 91 |
time_start = cpu_time(); |
| 92 |
for (size_t i = 0; i < num_repeat; ++i) { |
| 93 |
std::vector<THINGY> other_thingies(thingies); |
| 94 |
} |
| 95 |
time_stop = cpu_time(); |
| 96 |
|
| 97 |
timingData.copyTime = time_stop - time_start; |
| 98 |
} |
| 99 |
|
| 100 |
return timingData; |
| 101 |
} |
| 102 |
|
| 103 |
void print_timings(const std::string & typeName, const TimingData & newTiming, const TimingData & referenceTiming) |
| 104 |
{ |
| 105 |
std::cout << std::setprecision(3) << std::fixed; |
| 106 |
std::cout << "--------------------------------------------------------" << std::endl; |
| 107 |
std::cout << typeName << ": default init = " << newTiming.defaultInitTime << " s"; |
| 108 |
if (&newTiming == &referenceTiming) { |
| 109 |
std::cout << std::endl; |
| 110 |
} else { |
| 111 |
std::cout << " (" << std::showpos << (newTiming.defaultInitTime - referenceTiming.defaultInitTime) * 100. / referenceTiming.defaultInitTime |
| 112 |
<< "%)" << std::noshowpos << std::endl; |
| 113 |
} |
| 114 |
|
| 115 |
std::cout << typeName << ": push_back init = " << newTiming.pushBackInitTime << " s"; |
| 116 |
if (&newTiming == &referenceTiming) { |
| 117 |
std::cout << std::endl; |
| 118 |
} else { |
| 119 |
std::cout << " (" << std::showpos << (newTiming.pushBackInitTime - referenceTiming.pushBackInitTime) * 100. / referenceTiming.pushBackInitTime |
| 120 |
<< "%)" << std::noshowpos << std::endl; |
| 121 |
} |
| 122 |
|
| 123 |
std::cout << typeName << ": copy construction = " << newTiming.copyTime << " s"; |
| 124 |
if (&newTiming == &referenceTiming) { |
| 125 |
std::cout << std::endl; |
| 126 |
} else { |
| 127 |
std::cout << " (" << std::showpos << (newTiming.copyTime - referenceTiming.copyTime) * 100. / referenceTiming.copyTime |
| 128 |
<< "%)" << std::noshowpos << std::endl; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
TEST(POD, performanceOfPODvsClass) |
| 134 |
{ |
| 135 |
static_assert(std::is_trivial<PodThingy>::value, "PodThingy should be POD"); |
| 136 |
static_assert(!std::is_trivial<NonPodThingy>::value, "NonPodThingy should not be POD"); |
| 137 |
|
| 138 |
const size_t NUM_THINGIES = 1000000; |
| 139 |
const size_t NUM_REPEAT = 500; |
| 140 |
|
| 141 |
time_thingy<uint64_t>(NUM_THINGIES, NUM_REPEAT); |
| 142 |
const TimingData timingData_uint64 = time_thingy<uint64_t>(NUM_THINGIES, NUM_REPEAT); |
| 143 |
print_timings("uint64_t", timingData_uint64, timingData_uint64); |
| 144 |
|
| 145 |
time_thingy<PodThingy>(NUM_THINGIES, NUM_REPEAT); |
| 146 |
const TimingData timingData_PodThingy = time_thingy<PodThingy>(NUM_THINGIES, NUM_REPEAT); |
| 147 |
print_timings("PodThingy", timingData_PodThingy, timingData_uint64); |
| 148 |
|
| 149 |
time_thingy<NonPodThingy>(NUM_THINGIES, NUM_REPEAT); |
| 150 |
const TimingData timingData_NonPodThingy = time_thingy<NonPodThingy>(NUM_THINGIES, NUM_REPEAT); |
| 151 |
print_timings("NonPodThingy", timingData_NonPodThingy, timingData_uint64); |
| 152 |
} |
| 153 |
|