Created
January 10, 2017 15:15
-
-
Save JDevlieghere/72d745d144b6b9855b21e477fdbdccf1 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 FarApart | |
{ | |
char a; | |
char c[128]; | |
char b; | |
}; | |
struct CloseTogether | |
{ | |
char c[128]; | |
char a; | |
char b; | |
}; | |
template<typename T> | |
void benchmarkCache(T* t, int s) | |
{ | |
int j = 0; | |
for (int i = 0; i != s; ++i) { | |
if(t[i].a == t[i].b) | |
j++; | |
} | |
} | |
int main() { | |
constexpr auto LENGTH = 10000000; | |
CloseTogether* ct = new CloseTogether[LENGTH]; | |
FarApart* fa = new FarApart[LENGTH]; | |
std::cout << "Struct with members close together takes: "<< benchmark(benchmarkCache<CloseTogether>, ct, LENGTH).count() << std::endl; | |
std::cout << "Struct with members far apart takes: " << benchmark(benchmarkCache<FarApart>, fa, LENGTH).count() << std::endl; | |
delete[] ct; | |
delete[] fa; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment