cache benchmark and utils/time/timer improvement

This commit is contained in:
Marko Budiselic 2016-06-08 23:39:24 +02:00
parent c79706c249
commit 65b9af05d4
4 changed files with 62 additions and 12 deletions

View File

@ -0,0 +1,48 @@
#include <iostream>
#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<ms>([]() {
for (long i = 0; i < iterations_no; i++) {
data_short[i].a = data_short[i].b;
}
});
auto time_long = timer<ms>([]() {
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;
}

Binary file not shown.

View File

@ -1,19 +1,21 @@
#pragma once
#include <utility>
#include <chrono>
using time_point_t = std::chrono::high_resolution_clock::time_point;
#define duration(a) \
std::chrono::duration_cast<std::chrono::nanoseconds>(a).count()
#include <ratio>
#include <utility>
#define time_now() std::chrono::high_resolution_clock::now()
template<typename F, typename... Args>
double timer(F func, Args&&... args)
template <typename DurationUnit = std::chrono::nanoseconds>
auto to_duration(const std::chrono::duration<long, std::nano> &delta)
{
time_point_t start_time = time_now();
func(std::forward<Args>(args)...);
return duration(time_now() - start_time);
return std::chrono::duration_cast<DurationUnit>(delta).count();
}
template <typename DurationUnit, typename F, typename... Args>
auto timer(F func, Args &&... args)
{
auto start_time = time_now();
func(std::forward<Args>(args)...);
return to_duration<DurationUnit>(time_now() - start_time);
}

View File

@ -1,7 +1,7 @@
#pragma once
// USAGE:
// type_name<decltype<variable>>();
// type_name<decltype(variable)>();
// 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