forked from SciSharp/NumSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayTester.cs
More file actions
204 lines (147 loc) · 5.66 KB
/
ArrayTester.cs
File metadata and controls
204 lines (147 loc) · 5.66 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using System;
using System.Linq;
namespace NumSharp.Benchmark
{
public static class ArrayTester
{
public static void Access()
{
var A = new int[10000,10000];
int ALength = 10000 * 10000;
for (int idx = 0; idx < ALength;idx++)
{
int dim0 = idx / 10000;
int dim1 = idx % 10000;
A[dim0,dim1] = idx;
}
var watcher1 = new System.Diagnostics.Stopwatch();
watcher1.Start();
for (int idx = 0; idx < ALength;idx++)
{
int dim0 = idx / 10000;
int dim1 = idx % 10000;
int puffer = A[dim0,dim1];
A[dim0,dim1] = puffer;
}
watcher1.Stop();
A = null;
var B = new int[10000][];
B = B.Select(x => new int[10000]).ToArray();
for (int idx = 0; idx < ALength;idx++)
{
int dim0 = idx / 10000;
int dim1 = idx % 10000;
B[dim0][dim1] = idx;
}
for (int idx = 0; idx < ALength;idx++)
{
int dim0 = idx / 10000;
int dim1 = idx % 10000;
B[dim0][dim1] = idx;
}
var watcher2 = new System.Diagnostics.Stopwatch();
watcher2.Start();
for (int idx = 0; idx < ALength;idx++)
{
int dim0 = idx / 10000;
int dim1 = idx % 10000;
int puffer = B[dim0][dim1];
B[dim0][dim1] = puffer;
}
watcher2.Stop();
B = null;
var C = new int[ALength];
C = C.Select((x,idx) => idx).ToArray();
var watcher3 = new System.Diagnostics.Stopwatch();
watcher3.Start();
for (int idx = 0; idx < ALength;idx++)
{
int dim0 = idx / 10000;
int dim1 = idx % 10000;
int puffer = C[idx];
C[idx] = puffer;
}
watcher3.Stop();
}
public static void CheckPlusOperation()
{
var A = new double[1000][];
A = A.Select(x => new double[1000]).Select((x,idx) => x.Select((y,jdx) => (double)(idx + jdx) ).ToArray()).ToArray();
var B = new double[1000][];
B = B.Select(x => new double[1000]).Select((x,idx) => x.Select((y,jdx) => (double)(idx + jdx) ).ToArray()).ToArray();
var watch1 = new System.Diagnostics.Stopwatch();
watch1.Start();
var C = A.Select((x,idx) => x.Select((y,jdx) => y + A[idx][jdx]).ToArray()).ToArray();
watch1.Stop();
A = null;
B = null;
var a = new double[1000*1000];
a = a.Select((x,idx) => (double)idx).ToArray();
var b = new double[1000*1000];
b = b.Select((x,idx) => (double)idx).ToArray();
var watch2 = new System.Diagnostics.Stopwatch();
watch2.Start();
var c = a.Select((x,idx) => x + b[idx]).ToArray();
watch2.Stop();
a = null;
b = null;
}
public static void CheckMatrixMultiplication()
{
var A = new double[1000][];
A = A.Select(x => new double[1000]).Select((x,idx) => x.Select((y,jdx) => (double)(idx + jdx) ).ToArray()).ToArray();
var B = new double[1000][];
B = B.Select(x => new double[1000]).Select((x,idx) => x.Select((y,jdx) => (double)(idx + jdx) ).ToArray()).ToArray();
int numOfLines = A.GetLength(0);
int numOfColumns = A[0].GetLength(0);
int iterator = B[0].GetLength(0);
var result = new double[numOfLines][];
result = result.Select(x => new double[numOfColumns]).ToArray();
var watch1 = new System.Diagnostics.Stopwatch();
watch1.Start();
for (int idx = 0; idx < numOfLines;idx++)
{
for( int jdx = 0; jdx < numOfColumns; jdx++)
{
result[idx][jdx] = 0;
for (int kdx = 0; kdx < iterator; kdx++)
{
result[idx][jdx] += A[idx][kdx] * B[kdx][jdx];
}
}
}
watch1.Stop();
var a = new double[]{1,2,3,4,5,6,7,8,9};
var b = new double[]{8,0,4,2,1,0,8,1,0};
var c = new double[9];
for (int idx = 0; idx < 9;idx++)
{
int line = idx % 3;
int column = idx / 3;
c[idx] = 0;
for (int kdx = 0; kdx < 3;kdx++)
{
c[idx] += a[line + kdx * 3] * b[3 * column + kdx];
}
}
a = new double[1000*1000];
a = a.Select((x,idx) => (double)idx).ToArray();
b = new double[1000*1000];
b = b.Select((x,idx) => (double)idx).ToArray();
var resultArr = new double[a.Length];
var watch2 = new System.Diagnostics.Stopwatch();
watch2.Start();
for (int idx = 0; idx < a.Length;idx++)
{
int line = idx % 1000;
int column = idx / 1000;
resultArr[idx] = 0;
for (int kdx = 0; kdx < iterator;kdx++)
{
resultArr[idx] += a[line + kdx * 1000] * b[1000 * column + kdx];
}
}
watch2.Stop();
}
}
}