#include <stdint.h>
template<int BytesCount>
struct rawdata
{
char _[ BytesCount ];
inline char& operator[](int index)
{
return _[ index ];
}
};
//...
rawdata<4> genINC(rawdata<4> p)
{
rawdata<4> r = { p[ 0 ], p[ 1 ], p[ 2 ], p[ 3 ] };
++*( ( int* )&r[ 0 ] );
return r;
}
rawdata<4> genINC32(rawdata<4> p)
{
rawdata<4> res = p;
uint32_t* a = (uint32_t*)&res[0];
++*a;
return res;
}
#include <iostream>
#include <chrono>
template<int n>
std::ostream& operator<<(std::ostream& os, rawdata<n> p)
{
for(int i = 0; i < n; i++)
os << " - " << (int)p[i];
return os;
}
int main()
{
// Check that functions outputs the same things
rawdata<4> test = { 1, 0, 0, 0 };
rawdata<4> t1 = genINC(test);
rawdata<4> t2 = genINC32(test);
std::cout << "test" << test << "\n";
std::cout << "t1 " << t1 << "\n";
std::cout << "t2 " << t2 << "\n";
// Perf check
const uint64_t N = 10000000;
rawdata<4> val = test;
std::chrono::steady_clock::time_point start, end;
uint64_t timeINC = 0, timeINC32 = 0;
val[0] = 42;
start = std::chrono::steady_clock::now();
for(uint64_t t = 0; t < N; t++)
val = genINC(val);
end = std::chrono::steady_clock::now();
timeINC = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
val[0] = 42;
start = std::chrono::steady_clock::now();
for(uint64_t t = 0; t < N; t++)
val = genINC32(val);
end = std::chrono::steady_clock::now();
timeINC32 = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
std::cout << "INC : " << timeINC << "µs\n";
std::cout << "INC32: " << timeINC32 << "µs\n";
std::cout << "INC32 is " << (timeINC / float(timeINC32)) << "x faster than INC\n";
std::cout << "\n\n(unused" << val << ")\n";
}