-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmy_powers_acc_test.cpp
66 lines (55 loc) · 1.85 KB
/
my_powers_acc_test.cpp
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
#include <iomanip>
#include <iostream>
#include <string>
#include "my_intrinsics.h"
#include "my_type_functions.h"
#include "my_powers_acc.h"
using namespace std;
void list_acc_left(string& lhs, const string& rhs) {
lhs = string("(") + lhs + string(" ") + rhs + string(")");
}
void list_acc_right(const string& lhs, string& rhs) {
rhs = string("(") + lhs + string(" ") + rhs + string(")");
}
void multiply_acc(int& a, const int& b) {
a = a * b;
}
template<int modulus>
void mod_multiply_acc(int& a, const int& b) {
a = static_cast<int>((static_cast<long>(a) * b) % modulus);
}
void test_acc(void (*f)(int&, long, void (*)(int&, const int&)), string name) {
clock_t start, end;
start = clock();
int result = 2;
f(result, LONG_MAX, mod_multiply_acc<19599399173>);
end = clock();
cout << setw(25) << name << ": " << result << ", ";
cout << "clock ticks: " << (end - start) << endl;
}
void test_accumulate_acc(void (*f)(int&, int, long, void (*)(int&, const int&)), string name) {
clock_t start, end;
start = clock();
int result = 1;
f(result, 2, LONG_MAX, mod_multiply_acc<19599399173>);
end = clock();
cout << setw(25) << name << ": " << result << ", ";
cout << "clock ticks: " << (end - start) << endl;
}
int main() {
string a = "a";
power_left_associated_acc(a, 5, list_acc_left);
cout << a << endl;
a = "a";
power_right_associated_acc(a, 5, list_acc_right);
cout << a << endl;
test_accumulate_acc(power_accumulate_positive_acc, "power_accumulate_positive_acc");
test_accumulate_acc(power_accumulate_acc, "power_accumulate_acc");
test_acc(power_acc, "power_acc");
cout << "powers of two: " << endl;
for (int n = 0; n <= 10; ++n) {
int result = 2;
power_acc(result, n, multiply_acc, 1);
cout << setw(5) << n << ": " << result << endl;
}
}