Skip to content

Commit 682dbd4

Browse files
committed
Turlikov last fix
1 parent 2ea66f2 commit 682dbd4

File tree

1 file changed

+16
-47
lines changed

1 file changed

+16
-47
lines changed

Turlikov/Report_2/dop_task/main.cpp

Lines changed: 16 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
using namespace std;
1616
using namespace std::chrono;
1717

18-
#define ITERS 50000000
18+
#define ITERS 2000000
1919

2020
char arr_small[16] = {
2121
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4
@@ -37,7 +37,7 @@ char arr_big[256] = {
3737
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3838
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
3939
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
40-
4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
40+
4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8,
4141
};
4242

4343
unsigned char count_ones_1(unsigned long long n) {
@@ -58,7 +58,7 @@ unsigned char count_ones_2(unsigned long long n) {
5858
return w;
5959
}
6060

61-
unsigned char count_ones_3 (unsigned long long n) {
61+
unsigned char count_ones_3(unsigned long long n) {
6262
unsigned char w = 0;
6363
while (n > 0) {
6464
w += arr_small[n & 15];
@@ -76,55 +76,24 @@ unsigned char count_ones_4(unsigned long long n) {
7676
return w;
7777
}
7878

79-
int main() {
80-
81-
// алгоритм с делением
82-
cout << endl << "func: 1" << endl;
83-
for (int bit = 0; bit < 64; bit++) {
84-
int64_t n = (1LL << bit); // + rand() % (1LL << bit);
85-
auto t1 = high_resolution_clock::now();
86-
for (int i = 0; i < ITERS; i++, count_ones_1(n));
87-
auto t2 = high_resolution_clock::now();
88-
uint64_t duration = duration_cast<microseconds>(t2 - t1).count();
89-
cout << "bit: " << bit + 1;
90-
cout << "\tdur: " << (double) duration / ITERS << " us\n";
91-
}
92-
93-
// алгоритм с побитовыми операциями
94-
cout << endl << "func: 2" << endl;
95-
for (int bit = 0; bit < 64; bit++) {
96-
int64_t n = (1LL << bit); // + rand() % (1LL << bit);
97-
auto t1 = high_resolution_clock::now();
98-
for (int i = 0; i < ITERS; i++, count_ones_2(n));
99-
auto t2 = high_resolution_clock::now();
100-
uint64_t duration = duration_cast<microseconds>(t2 - t1).count();
101-
cout << "bit: " << bit + 1;
102-
cout << "\tdur: " << (double) duration / ITERS << " us\n";
103-
}
104-
105-
// алгоритм со вспомогательной таблицей (маленькой)
106-
cout << endl << "func: 3" << endl;
79+
void benchmark(string str, unsigned char (*cum)(uint64_t)) {
80+
cout << endl << str << endl;
10781
for (int bit = 0; bit < 64; bit++) {
10882
int64_t n = (1LL << bit); // + rand() % (1LL << bit);
10983
auto t1 = high_resolution_clock::now();
110-
for (int i = 0; i < ITERS; i++, count_ones_3(n));
84+
for (int i = 0; i < ITERS; i++) cum(n);
11185
auto t2 = high_resolution_clock::now();
112-
uint64_t duration = duration_cast<microseconds>(t2 - t1).count();
113-
cout << "bit: " << bit + 1;
114-
cout << "\tdur: " << (double) duration / ITERS << " us\n";
115-
}
116-
117-
// алгоритм со вспомогательной таблицей (большой)
118-
cout << endl << "func: 4" << endl;
119-
for (int bit = 0; bit < 64; bit++) {
120-
int64_t n = (1LL << bit); // + rand() % (1LL << bit);
121-
auto t1 = high_resolution_clock::now();
122-
for (int i = 0; i < ITERS; i++, count_ones_4(n));
123-
auto t2 = high_resolution_clock::now();
124-
uint64_t duration = duration_cast<microseconds>(t2 - t1).count();
125-
cout << "bit: " << bit + 1;
126-
cout << "\tdur: " << (double) duration / ITERS << " us\n";
86+
int64_t duration = duration_cast<microseconds>(t2 - t1).count();
87+
cout << "bit " << bit + 1
88+
<< ": dur " << (double) duration / ITERS
89+
<< " us" << endl;
12790
}
91+
}
12892

93+
int main() {
94+
benchmark("func: 1", count_ones_1);
95+
benchmark("func: 2", count_ones_2);
96+
benchmark("func: 3", count_ones_3);
97+
benchmark("func: 4", count_ones_4);
12998
return 0;
13099
}

0 commit comments

Comments
 (0)