diff --git a/src/experimental/cache_benchmark.cpp b/src/experimental/cache_benchmark.cpp new file mode 100644 index 000000000..ed5e132f0 --- /dev/null +++ b/src/experimental/cache_benchmark.cpp @@ -0,0 +1,48 @@ +#include + +#include "utils/time/timer.hpp" + +using std::cout; +using std::endl; + +using ns = std::chrono::nanoseconds; +using ms = std::chrono::milliseconds; + +constexpr long iterations_no = 10 * 1024 * 1024; + +struct DataShort +{ + long a; + long b; +}; + +struct DataLong +{ + long a; + long b; + long c; + long d; +}; + +DataShort *data_short = new DataShort[iterations_no]; +DataLong *data_long = new DataLong[iterations_no]; + +int main() +{ + auto time_short = timer([]() { + for (long i = 0; i < iterations_no; i++) { + data_short[i].a = data_short[i].b; + } + }); + + auto time_long = timer([]() { + for (long i = 0; i < iterations_no; i++) { + data_long[i].a = data_long[i].b; + } + }); + + cout << "Time short: " << time_short << " ms"<< endl; + cout << "Time long: " << time_long << " ms" << endl; + + return 0; +} diff --git a/src/experimental/cache_benchmark.run b/src/experimental/cache_benchmark.run new file mode 100755 index 000000000..b2b470c00 Binary files /dev/null and b/src/experimental/cache_benchmark.run differ diff --git a/src/utils/time/timer.hpp b/src/utils/time/timer.hpp index 28e337ac2..eb53893d8 100644 --- a/src/utils/time/timer.hpp +++ b/src/utils/time/timer.hpp @@ -1,19 +1,21 @@ #pragma once -#include #include - -using time_point_t = std::chrono::high_resolution_clock::time_point; - -#define duration(a) \ - std::chrono::duration_cast(a).count() +#include +#include #define time_now() std::chrono::high_resolution_clock::now() -template -double timer(F func, Args&&... args) +template +auto to_duration(const std::chrono::duration &delta) { - time_point_t start_time = time_now(); - func(std::forward(args)...); - return duration(time_now() - start_time); + return std::chrono::duration_cast(delta).count(); +} + +template +auto timer(F func, Args &&... args) +{ + auto start_time = time_now(); + func(std::forward(args)...); + return to_duration(time_now() - start_time); } diff --git a/src/utils/type_discovery.hpp b/src/utils/type_discovery.hpp index 9d49a2665..9368afe66 100644 --- a/src/utils/type_discovery.hpp +++ b/src/utils/type_discovery.hpp @@ -1,7 +1,7 @@ #pragma once // USAGE: -// type_name>(); +// type_name(); // current solution has been taken from http://stackoverflow.com/questions/81870/is-it-possible-to-print-a-variables-type-in-standard-c // TODO: create more appropriate solution