Created
April 3, 2019 01:44
-
-
Save dgski/725473353ce87fff785ec4161a5e2d65 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "benchmark.h" | |
#include <iostream> | |
#include <fstream> | |
constexpr auto LENGTH = 10000000; | |
constexpr auto ITERATIONS = 100; | |
template<int N> | |
struct LargeOffset { | |
char a[N]; | |
char c; | |
}; | |
template<int N> | |
struct SmallOffset { | |
char c; | |
char a[N]; | |
}; | |
template <typename T> | |
void benchmarkOffset(T* t, int s) { | |
for (int i = 0; i != s; ++i) { | |
t[i].c = 's'; | |
} | |
} | |
template <int N> | |
std::pair<double, double> runBenchmark() { | |
SmallOffset<N>* soa = new SmallOffset<N>[LENGTH]; | |
LargeOffset<N>* loa = new LargeOffset<N>[LENGTH]; | |
const double d1 = benchmark(benchmarkOffset<SmallOffset<N>>, soa, LENGTH).count(); | |
const double d2 = benchmark(benchmarkOffset<LargeOffset<N>>, loa, LENGTH).count(); | |
delete[] soa; | |
delete[] loa; | |
return {d1, d2}; | |
} | |
template <int N> | |
void run_benchmarks() { | |
double s1 = 0; | |
double s2 = 0; | |
for (int i = 0; i != ITERATIONS; ++i) { | |
auto p = runBenchmark<N>(); | |
s1 += p.first; | |
s2 += p.second; | |
} | |
s1 = s1 / ITERATIONS; | |
s2 = s2 / ITERATIONS; | |
std::cout << N << ":" << std::endl; | |
std::cout << "Small offset: " << s1 << std::endl; | |
std::cout << "Large offset: " << s2 << std::endl; | |
{ | |
std::ofstream outfile; | |
outfile.open("results.csv", std::ios_base::app); | |
outfile << N << ',' << s1 << ',' << s2 << ',' << (s2-s1) << std::endl; | |
} | |
run_benchmarks<N-1>(); | |
} | |
template<> | |
void run_benchmarks<0>() { | |
// terminate loop | |
} | |
int main() { | |
{ | |
std::ofstream outfile; | |
outfile.open("results.csv"); | |
outfile << "Byte Offset, Large, Small, Difference" << std::endl; | |
} | |
run_benchmarks<130>(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment