-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimization.cpp
More file actions
79 lines (75 loc) · 2.36 KB
/
Copy pathoptimization.cpp
File metadata and controls
79 lines (75 loc) · 2.36 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
//Optimization
#include <iostream>
#include <chrono>
using namespace std;
using namespace chrono;
namespace _2pow {
const unsigned int methods =3;
#define METHOD(num) unsigned long long int method##num(int pow)
METHOD(1){
long double ret = 1;
for(int i=0;i<pow;i++)
ret*=2;
return ret;
}
METHOD(2){
return pow ==0 ? 1 : 2*method2(pow-1);
}
METHOD(3) {
return 0x1 << pow;
}
#undef METHOD
};
int main(){
#define __call(num) method##num
{
high_resolution_clock::time_point t1 = high_resolution_clock::now();
for (int i = 0; i < 10000; ++i)
{
_2pow::__call(1)(i);
}
high_resolution_clock::time_point t2 = high_resolution_clock::now();
auto duration = duration_cast<nanoseconds>(t2 - t1).count();
cout << "\t" << duration << " NanoSeconds \n";
duration = duration_cast<microseconds>(t2 - t1).count();
cout << "\t" << duration << " MicroSeconds \n";
duration = duration_cast<milliseconds>(t2 - t1).count();
cout << "\t" << duration << " MilliSeconds \n";
duration = duration_cast<seconds>(t2 - t1).count();
cout << "\t" << duration << " Seconds \n";
}
{
high_resolution_clock::time_point t1 = high_resolution_clock::now();
for (int i = 0; i < 10000; ++i)
{
_2pow::__call(2)(i);
}
high_resolution_clock::time_point t2 = high_resolution_clock::now();
auto duration = duration_cast<nanoseconds>(t2 - t1).count();
cout << "\t" << duration << " NanoSeconds \n";
duration = duration_cast<microseconds>(t2 - t1).count();
cout << "\t" << duration << " MicroSeconds \n";
duration = duration_cast<milliseconds>(t2 - t1).count();
cout << "\t" << duration << " MilliSeconds \n";
duration = duration_cast<seconds>(t2 - t1).count();
cout << "\t" << duration << " Seconds \n";
}
{
high_resolution_clock::time_point t1 = high_resolution_clock::now();
for (int i = 0; i < 10000; ++i)
{
_2pow::__call(3)(i);
}
high_resolution_clock::time_point t2 = high_resolution_clock::now();
auto duration = duration_cast<nanoseconds>(t2 - t1).count();
cout << "\t" << duration << " NanoSeconds \n";
duration = duration_cast<microseconds>(t2 - t1).count();
cout << "\t" << duration << " MicroSeconds \n";
duration = duration_cast<milliseconds>(t2 - t1).count();
cout << "\t" << duration << " MilliSeconds \n";
duration = duration_cast<seconds>(t2 - t1).count();
cout << "\t" << duration << " Seconds \n";
}
#undef __call
return 0;
}