1+
2+ using System ;
3+ using System . Linq ;
4+
5+ namespace NumSharp . Benchmark
6+ {
7+ public static class ArrayTester
8+ {
9+ public static void Access ( )
10+ {
11+ var A = new int [ 10000 , 10000 ] ;
12+ int ALength = 10000 * 10000 ;
13+
14+ for ( int idx = 0 ; idx < ALength ; idx ++ )
15+ {
16+ int dim0 = idx / 10000 ;
17+ int dim1 = idx % 10000 ;
18+ A [ dim0 , dim1 ] = idx ;
19+ }
20+
21+ var watcher1 = new System . Diagnostics . Stopwatch ( ) ;
22+
23+ watcher1 . Start ( ) ;
24+
25+ for ( int idx = 0 ; idx < ALength ; idx ++ )
26+ {
27+ int dim0 = idx / 10000 ;
28+ int dim1 = idx % 10000 ;
29+ int puffer = A [ dim0 , dim1 ] ;
30+ A [ dim0 , dim1 ] = puffer ;
31+ }
32+
33+ watcher1 . Stop ( ) ;
34+
35+ A = null ;
36+
37+ var B = new int [ 10000 ] [ ] ;
38+
39+ B = B . Select ( x => new int [ 10000 ] ) . ToArray ( ) ;
40+
41+ for ( int idx = 0 ; idx < ALength ; idx ++ )
42+ {
43+ int dim0 = idx / 10000 ;
44+ int dim1 = idx % 10000 ;
45+ B [ dim0 ] [ dim1 ] = idx ;
46+ }
47+
48+ for ( int idx = 0 ; idx < ALength ; idx ++ )
49+ {
50+ int dim0 = idx / 10000 ;
51+ int dim1 = idx % 10000 ;
52+ B [ dim0 ] [ dim1 ] = idx ;
53+ }
54+
55+ var watcher2 = new System . Diagnostics . Stopwatch ( ) ;
56+
57+ watcher2 . Start ( ) ;
58+
59+ for ( int idx = 0 ; idx < ALength ; idx ++ )
60+ {
61+ int dim0 = idx / 10000 ;
62+ int dim1 = idx % 10000 ;
63+ int puffer = B [ dim0 ] [ dim1 ] ;
64+ B [ dim0 ] [ dim1 ] = puffer ;
65+ }
66+
67+ watcher2 . Stop ( ) ;
68+
69+ B = null ;
70+
71+ var C = new int [ ALength ] ;
72+
73+ C = C . Select ( ( x , idx ) => idx ) . ToArray ( ) ;
74+
75+ var watcher3 = new System . Diagnostics . Stopwatch ( ) ;
76+
77+ watcher3 . Start ( ) ;
78+
79+ for ( int idx = 0 ; idx < ALength ; idx ++ )
80+ {
81+ int dim0 = idx / 10000 ;
82+ int dim1 = idx % 10000 ;
83+ int puffer = C [ idx ] ;
84+ C [ idx ] = puffer ;
85+ }
86+
87+ watcher3 . Stop ( ) ;
88+
89+ }
90+ public static void CheckPlusOperation ( )
91+ {
92+ var A = new double [ 1000 ] [ ] ;
93+ A = A . Select ( x => new double [ 1000 ] ) . Select ( ( x , idx ) => x . Select ( ( y , jdx ) => ( double ) ( idx + jdx ) ) . ToArray ( ) ) . ToArray ( ) ;
94+
95+ var B = new double [ 1000 ] [ ] ;
96+ B = B . Select ( x => new double [ 1000 ] ) . Select ( ( x , idx ) => x . Select ( ( y , jdx ) => ( double ) ( idx + jdx ) ) . ToArray ( ) ) . ToArray ( ) ;
97+
98+ var watch1 = new System . Diagnostics . Stopwatch ( ) ;
99+ watch1 . Start ( ) ;
100+
101+ var C = A . Select ( ( x , idx ) => x . Select ( ( y , jdx ) => y + A [ idx ] [ jdx ] ) . ToArray ( ) ) . ToArray ( ) ;
102+
103+ watch1 . Stop ( ) ;
104+
105+ A = null ;
106+ B = null ;
107+
108+ var a = new double [ 1000 * 1000 ] ;
109+ a = a . Select ( ( x , idx ) => ( double ) idx ) . ToArray ( ) ;
110+
111+ var b = new double [ 1000 * 1000 ] ;
112+ b = b . Select ( ( x , idx ) => ( double ) idx ) . ToArray ( ) ;
113+
114+ var watch2 = new System . Diagnostics . Stopwatch ( ) ;
115+ watch2 . Start ( ) ;
116+
117+ var c = a . Select ( ( x , idx ) => x + b [ idx ] ) . ToArray ( ) ;
118+
119+ watch2 . Stop ( ) ;
120+
121+ a = null ;
122+ b = null ;
123+
124+
125+ }
126+ public static void CheckMatrixMultiplication ( )
127+ {
128+ var A = new double [ 1000 ] [ ] ;
129+ A = A . Select ( x => new double [ 1000 ] ) . Select ( ( x , idx ) => x . Select ( ( y , jdx ) => ( double ) ( idx + jdx ) ) . ToArray ( ) ) . ToArray ( ) ;
130+
131+ var B = new double [ 1000 ] [ ] ;
132+ B = B . Select ( x => new double [ 1000 ] ) . Select ( ( x , idx ) => x . Select ( ( y , jdx ) => ( double ) ( idx + jdx ) ) . ToArray ( ) ) . ToArray ( ) ;
133+
134+ int numOfLines = A . GetLength ( 0 ) ;
135+ int numOfColumns = A [ 0 ] . GetLength ( 0 ) ;
136+
137+ int iterator = B [ 0 ] . GetLength ( 0 ) ;
138+
139+ var result = new double [ numOfLines ] [ ] ;
140+ result = result . Select ( x => new double [ numOfColumns ] ) . ToArray ( ) ;
141+
142+ var watch1 = new System . Diagnostics . Stopwatch ( ) ;
143+ watch1 . Start ( ) ;
144+
145+ for ( int idx = 0 ; idx < numOfLines ; idx ++ )
146+ {
147+ for ( int jdx = 0 ; jdx < numOfColumns ; jdx ++ )
148+ {
149+ result [ idx ] [ jdx ] = 0 ;
150+ for ( int kdx = 0 ; kdx < iterator ; kdx ++ )
151+ {
152+ result [ idx ] [ jdx ] += A [ idx ] [ kdx ] * B [ kdx ] [ jdx ] ;
153+ }
154+ }
155+ }
156+
157+ watch1 . Stop ( ) ;
158+
159+ var a = new double [ ] { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ;
160+ var b = new double [ ] { 8 , 0 , 4 , 2 , 1 , 0 , 8 , 1 , 0 } ;
161+
162+ var c = new double [ 9 ] ;
163+
164+ for ( int idx = 0 ; idx < 9 ; idx ++ )
165+ {
166+ int line = idx % 3 ;
167+ int column = idx / 3 ;
168+
169+ c [ idx ] = 0 ;
170+ for ( int kdx = 0 ; kdx < 3 ; kdx ++ )
171+ {
172+ c [ idx ] += a [ line + kdx * 3 ] * b [ 3 * column + kdx ] ;
173+ }
174+ }
175+
176+ a = new double [ 1000 * 1000 ] ;
177+ a = a . Select ( ( x , idx ) => ( double ) idx ) . ToArray ( ) ;
178+
179+ b = new double [ 1000 * 1000 ] ;
180+ b = b . Select ( ( x , idx ) => ( double ) idx ) . ToArray ( ) ;
181+
182+ var resultArr = new double [ a . Length ] ;
183+
184+ var watch2 = new System . Diagnostics . Stopwatch ( ) ;
185+ watch2 . Start ( ) ;
186+
187+
188+ for ( int idx = 0 ; idx < a . Length ; idx ++ )
189+ {
190+ int line = idx % 1000 ;
191+ int column = idx / 1000 ;
192+
193+ resultArr [ idx ] = 0 ;
194+ for ( int kdx = 0 ; kdx < iterator ; kdx ++ )
195+ {
196+ resultArr [ idx ] += a [ line + kdx * 1000 ] * b [ 1000 * column + kdx ] ;
197+ }
198+ }
199+
200+ watch2 . Stop ( ) ;
201+
202+ }
203+ }
204+ }
0 commit comments