I have written an std::vectort<T>
performance test (T in int
, bool
, char
, int_fast8_t
, uint_fast8_t
). It has two phases: writing and reading. I’m not afraid of writing being optimized away as I later read the container. However when I force the read to be not optimized away I suspect that I destruct other optimizations as well. The kind that could happen in a “normal” program. I base my suspicion on the fact that writing is many times faster than reading.
I prevent reading being optimized away with this function that I copied from somewhere:
template <typename T>
inline void doNotOptimizeAway(T&& datum) {
asm volatile ("" : "+r" (datum));
}
I use it as:
for(T val: vec)
doNotOptimizeAway(val);
Test results are (cut, formatted):
int: write 84 ms; read 145 ms.
bool: write 414 ms; read 458 ms.
char: write 21 ms; read 141 ms.
int_fast8_t: write 21 ms; read 141 ms.
uint_fast8_t: write 21 ms; read 140 ms.
Good signs:
char
,int_fast8_t
,uint_fast8_t
reads and writes are of the same speed.- The write for these types is quite precisely 4 times faster than for
int
. bool
is very slow as expected, because of storage optimized specialization.
Bad signs:
- Reading is slower than writing for all non-
bool
cases.
Complete source: https://pastebin.com/weN6wtpq
The question: How can I prevent the reads being optimized away without destroying other optimizations?