-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
161 lines (136 loc) · 4.12 KB
/
main.cpp
File metadata and controls
161 lines (136 loc) · 4.12 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <benchmark/benchmark.h>
#include <time.h>
#include <algorithm>
#include <atomic>
#include <cassert>
#include <cstdlib>
#include <iostream>
#include <numeric>
#include <thread>
#include <vector>
#include "benchmark_memory.h"
typedef std::vector<int> VALS;
int max_sub_array_greedy(const VALS& vals, int start_at, int end_at, int& start_index, int& end_index)
{
int max = vals[0];
start_index = end_index = start_at;
for(int i = start_at; i < end_at; ++i)
{
int s = vals[i];
for(int j = i; j < end_at; ++j)
{
if(j != i)
s+=vals[j];
if(s > max)
{
max = s;
start_index = i;
end_index = j;
}
}
}
return max;
}
static const int DATA_LEN = 100;
static void BM_greedy(benchmark::State& s)
{
VALS data;
for (int i = 0; i < DATA_LEN; i++) {
data.push_back(rand());
}
MEMORY_MONITOR_BEGIN // my custom counter for capturing mem usage - start
int start_index = 0, end_index = 0, max = std::numeric_limits<int>::min();
for(auto _ : s)
{
// UNCOMMENT THIS (introduces slow down by performing same code a number of times - informed by the demo machine capabilities)
//for(int i = 0; i < 10 * std::thread::hardware_concurrency(); i++)
{
max = max_sub_array_greedy(data, 0, data.size(), start_index, end_index);
}
}
MEMORY_MONITOR_END // my custom counter for capturing mem usage - end
std::cout << max << std::endl;
}
BENCHMARK(BM_greedy);
inline int max_crossing_sub_array(const VALS& vals, int start_at, int mid, int end_at, int& start_index, int& end_index)
{
int left_max = vals[mid];
int left_start_index = mid;
int s = 0;
for(int l = mid; l >= start_at; --l)
{
s+=vals[l];
if(s > left_max)
{
left_max = s;
left_start_index = l;
}
}
int right_end_index = -1;
int right_max = vals[mid+1];
s = 0;
for(int r = mid+1; r <= end_at; ++r)
{
s+=vals[r];
if(s > right_max)
{
right_max = s;
right_end_index = r;
}
}
start_index = left_start_index;
end_index = right_end_index;
return left_max + right_max;
}
int max_sub_array_logarithmic(const VALS& vals, int start_at, int end_at, int& start_index, int& end_index)
{
if(start_at == end_at)
{
start_index = end_index = start_at;
return vals[start_at];
}
int sze = end_at - start_at;
int mid = start_at + (sze >> 1);
int left_start = 0, left_end = 0;
int left_max = max_sub_array_logarithmic(vals, start_at, mid, left_start, left_end);
int right_start = 0, right_end = 0;
int right_max = max_sub_array_logarithmic(vals, mid+1, end_at, right_start, right_end);
int cross_start = 0, cross_end = 0;
int cross_max = max_crossing_sub_array(vals, start_at, mid, end_at, cross_start, cross_end);
int max = 0;
if(left_max > right_max && left_max > cross_max)
{
start_index = left_start;
end_index = left_end;
max = left_max;
}
else if(right_max > left_max && right_max > cross_max)
{
start_index = right_start;
end_index = right_end;
max = right_max;
}
else
{
start_index = cross_start;
end_index = cross_end;
max = cross_max;
}
return max;
}
static void BM_logarithmic(benchmark::State& s)
{
VALS data;
for (int i = 0; i < DATA_LEN; i++) {
data.push_back(rand());
}
MEMORY_MONITOR_BEGIN // my custom counter for capturing mem usage - start
int start_index = 0, end_index = 0, max = std::numeric_limits<int>::min();
for (auto _ : s) {
max = max_sub_array_logarithmic(data, 0, data.size() - 1, start_index, end_index);
}
MEMORY_MONITOR_END // my custom counter for capturing mem usage - end
std::cout << max << std::endl;
}
BENCHMARK(BM_logarithmic);
BENCHMARK_MAIN();