-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.h
75 lines (62 loc) · 1.79 KB
/
benchmark.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//
// Created by user on 12/5/22.
//
#ifndef NEON_INTRINSICS_EXERCISE_BENCHMARK_H
#define NEON_INTRINSICS_EXERCISE_BENCHMARK_H
#include <functional>
#include <vector>
#include <algorithm>
#include <ctime>
#include <random>
#ifdef __ANDROID__
#include <android/log.h>
#define TAG "neon_test"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,TAG ,__VA_ARGS__)
#else
#define LOGD(...) printf(__VA_ARGS__)
#endif
template <typename duration_type = std::chrono::microseconds>
auto benchmark(std::function<void()> fun,
int call_count = 1
)
{
using namespace std::chrono;
duration_type total_time{0};
for(auto i = 0; i < call_count; ++i)
{
const auto start = high_resolution_clock::now();
fun();
const auto end = high_resolution_clock::now();
total_time += duration_cast<duration_type>(end - start);
}
auto average_time = total_time / call_count;
return std::pair{total_time, average_time};
}
template <typename T>
std::vector<T> buildRandomVector(size_t size, T min_val = 0, T max_val=std::numeric_limits<T>::max())
{
std::random_device rnd_device;
std::mt19937 mersenne_engine {rnd_device()}; // Generates random integers
std::uniform_int_distribution<T> dist{min_val, max_val};
auto gen = [&dist, &mersenne_engine](){
return dist(mersenne_engine);
};
std::vector<T> v(size);
std::generate(v.begin(), v.end(), gen);
return v;
}
template <typename T>
bool is_same_vector(const std::vector<T>& lfh, const std::vector<T>& rhs)
{
if(lfh.size() != rhs.size()){
return false;
}
for(int i = 0; i < lfh.size(); ++i)
{
if(fabs(lfh[i] - rhs[i]) > 1e-2){
return false;
}
}
return true;
}
#endif//NEON_INTRINSICS_EXERCISE_BENCHMARK_H