This repository has been archived by the owner on Oct 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.h
94 lines (77 loc) · 2.23 KB
/
common.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#ifndef COMMON_INC
#define COMMON_INC
// NOTE: NDEBUG is standard !
#if !defined( _DEBUG ) && !defined( DEBUG ) && !defined(NDEBUG)
#define NDEBUG
#endif // !_DEBUG and !DEBUG and NDEBUG
#ifdef __clang__
#pragma clang system_header
#endif // __clang__
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS 1
#endif // _CRT_SECURE_NO_WARNINGS
/// ---------------------------------------------------------------------
#include <assert.h>
#include <malloc.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/// ---------------------------------------------------------------------
/// ---------------------------------------------------------------------
#ifdef __cplusplus
#include <vector>
#include <array>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "dbj--nanolib/dbj++tu.h"
namespace dbj {
struct collector final {
char name_[0xFF]{ 0 };
float min_{};
float max_{};
int count_{};
explicit collector(const char* newname) noexcept : min_(0), max_(0), count_(0) {
strncpy_s(name_, newname, strlen(newname));
}
void add(float new_time_)
{
if (count_ == 0) {
min_ = new_time_;
max_ = new_time_;
count_ += 1;
return;
}
count_ += 1;
if (new_time_ < min_)
min_ = new_time_;
else if (new_time_ > max_)
max_ = new_time_;
else {/* no op */ }
}
static void report(collector& clctr_, void (*cb) (const char*, float, float, int)) {
cb(clctr_.name_, clctr_.min_, clctr_.max_, clctr_.count_);
}
};
/// ---------------------------------------------------------------------
static inline int randomizer(int max_ = 0xFF, int min_ = 1)
{
static auto _ = [] {
srand((unsigned)time(NULL)); return true;
}();
return (rand() % max_ + min_);
}
/// ---------------------------------------------------------------------
static inline auto driver = [](collector& clctr_, auto specimen)
{
volatile clock_t time_point_ = clock();
specimen();
/// float rez = (float)(clock() - time_point_) / CLOCKS_PER_SEC;
clctr_.add((float)(clock() - time_point_) / CLOCKS_PER_SEC);
/// printf("%12s %.3f sec ", prompt_, rez);
};
}
/// ---------------------------------------------------------------------
#endif // __cplusplus
#endif // !COMMON_INC