-
Notifications
You must be signed in to change notification settings - Fork 17
/
Performace_test.cpp
116 lines (111 loc) · 2.5 KB
/
Performace_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
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
#include <cstdio>
#include <cstring>
#include <ctime>
#include <chrono>
using namespace std;
class CVect{
public:
double *data;
int m_Size;
int m_Max;
void Init();
void Free();
CVect();
CVect(int size, double value=0.0);
CVect(int size, const double *pdata);
CVect(const CVect& arr);
CVect operator +(const CVect &v);
void print();
~CVect();
};
void CVect::Init()
{
this->m_Size = 0;
this->m_Max = 1;
this->data = new double[m_Max];
}
void CVect::Free()
{
delete []this->data;
}
CVect::CVect()
{
this->Init();
}
CVect::CVect(int size, double value)
{
if(0 == size)
this->Init();
else
{
this->m_Size = size;
this->m_Max = size;
data = new double[m_Max];
for(int i = 0; i < this->m_Size; ++i)
data[i] = value;
}
}
CVect::CVect(const CVect& arr)
{
this->m_Size = arr.m_Size;
this->m_Max = arr.m_Max;
this->data = new double[this->m_Max];
memcpy(data,arr.data,arr.m_Size*sizeof(double));
}
CVect::CVect(int size, const double *pdata)
{
printf("CVect::CVect Init\n");
this->m_Size = size;
this->m_Max = size;
this->data = new double[this->m_Max];
memcpy(data,pdata,size*sizeof(double));
}
void CVect::print()
{
if(0 == this->m_Size)
printf("Error: Empty array\n");
else
{
for(int i = 0; i < this->m_Size; ++i)
printf("%.3f ",this->data[i]);
printf("\n");
}
}
CVect CVect::operator +(const CVect &v)
{
CVect tmp = v;
auto start1 = std::chrono::system_clock::now();
for(int j = 0; j < 10000; j++)
{
for(int i = 0; i < this->m_Size; ++i)
tmp.data[i] = this->data[i] + v.data[i];
}
auto end1 = std::chrono::system_clock::now();
auto elapsed1 = end1 - start1;
printf("Using Index: %li ticks\n",elapsed1.count());
auto start = std::chrono::system_clock::now();
for(int j = 0; j < 10000; j++)
{
double *pout = tmp.data;
double *pthis = this->data;
double *pv = v.data;
for(double *PEnd = &tmp.data[tmp.m_Size]; pout < PEnd ; )
*pout++ = *pthis++ + *pv++;
}
auto end = std::chrono::system_clock::now();
auto elapsed = end - start;
printf("Using Pointer: %li ticks\n",elapsed.count());
printf("Index/Pointer: %.3f\n",double(elapsed1.count())/double(elapsed.count()));
return tmp;
}
CVect::~CVect()
{
this->Free();
}
int main()
{
CVect a1(100,1);
CVect a2 = a1;
CVect a3 = a1 + a2;
return 0;
}