Created
January 10, 2017 14:52
-
-
Save JDevlieghere/3fb99af17935b1ed288f66bb480fedc7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <chrono> | |
#include <iostream> | |
#include <type_traits> | |
template <class F, class... Args> | |
std::chrono::duration<double> | |
benchmark(F&& f, Args&&... args) | |
{ | |
using clock = std::conditional<std::chrono::high_resolution_clock::is_steady, | |
std::chrono::high_resolution_clock, | |
std::chrono::steady_clock>::type; | |
const auto t0 = clock::now(); | |
std::forward<F>(f)(std::forward<Args>(args)...); | |
return std::chrono::duration<double>(clock::now() - t0); | |
} | |
struct LargeOffset | |
{ | |
char a[128]; | |
char c; | |
}; | |
struct SmallOffset | |
{ | |
char c; | |
char a[128]; | |
}; | |
template<typename T> | |
void benchmarkOffset(T* t, int s) | |
{ | |
for (int i = 0; i != s; ++i) { | |
t[i].c = 's'; | |
} | |
} | |
int main() { | |
constexpr auto LENGTH = 10000000; | |
SmallOffset* soa = new SmallOffset[LENGTH]; | |
LargeOffset* loa = new LargeOffset[LENGTH]; | |
std::cout << "Struct with small offset takes: " << benchmark(benchmarkOffset<SmallOffset>, soa, LENGTH).count() << std::endl; | |
std::cout << "Struct with large offset takes: "<< benchmark(benchmarkOffset<LargeOffset>, loa, LENGTH).count() << std::endl; | |
delete[] soa; | |
delete[] loa; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment