cache benchmark and utils/time/timer improvement
This commit is contained in:
parent
c79706c249
commit
65b9af05d4
48
src/experimental/cache_benchmark.cpp
Normal file
48
src/experimental/cache_benchmark.cpp
Normal 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;
|
||||||
|
}
|
BIN
src/experimental/cache_benchmark.run
Executable file
BIN
src/experimental/cache_benchmark.run
Executable file
Binary file not shown.
@ -1,19 +1,21 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <utility>
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
#include <ratio>
|
||||||
using time_point_t = std::chrono::high_resolution_clock::time_point;
|
#include <utility>
|
||||||
|
|
||||||
#define duration(a) \
|
|
||||||
std::chrono::duration_cast<std::chrono::nanoseconds>(a).count()
|
|
||||||
|
|
||||||
#define time_now() std::chrono::high_resolution_clock::now()
|
#define time_now() std::chrono::high_resolution_clock::now()
|
||||||
|
|
||||||
template<typename F, typename... Args>
|
template <typename DurationUnit = std::chrono::nanoseconds>
|
||||||
double timer(F func, Args&&... args)
|
auto to_duration(const std::chrono::duration<long, std::nano> &delta)
|
||||||
{
|
{
|
||||||
time_point_t start_time = time_now();
|
return std::chrono::duration_cast<DurationUnit>(delta).count();
|
||||||
func(std::forward<Args>(args)...);
|
}
|
||||||
return duration(time_now() - start_time);
|
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
// USAGE:
|
// 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
|
// 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
|
// TODO: create more appropriate solution
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user