Skip to content

Commit 96caef8

Browse files
committed
Add benchmarks
1 parent f5c2c2b commit 96caef8

File tree

1 file changed

+159
-0
lines changed

1 file changed

+159
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var Float32Array = require( '@stdlib/array/float32' );
25+
var Complex64 = require( '@stdlib/complex/float32' );
26+
var isComplex64 = require( '@stdlib/assert/is-complex64' );
27+
var isArray = require( '@stdlib/assert/is-array' );
28+
var pkg = require( './../package.json' ).name;
29+
var Complex64Array = require( './../lib' );
30+
31+
32+
// MAIN //
33+
34+
bench( pkg+':get', function benchmark( b ) {
35+
var arr;
36+
var N;
37+
var z;
38+
var i;
39+
40+
arr = [];
41+
for ( i = 0; i < 10; i++ ) {
42+
arr.push( new Complex64( i, i ) );
43+
}
44+
arr = new Complex64Array( arr );
45+
N = arr.length;
46+
47+
b.tic();
48+
for ( i = 0; i < b.iterations; i++ ) {
49+
z = arr.get( i%N );
50+
if ( typeof z !== 'object' ) {
51+
b.fail( 'should return an object' );
52+
}
53+
}
54+
b.toc();
55+
if ( !isComplex64( z ) ) {
56+
b.fail( 'should return a complex number' );
57+
}
58+
b.pass( 'benchmark finished' );
59+
b.end();
60+
});
61+
62+
bench( pkg+'::memory_reuse:get', function benchmark( b ) {
63+
var arr;
64+
var out;
65+
var N;
66+
var z;
67+
var i;
68+
69+
arr = [];
70+
for ( i = 0; i < 10; i++ ) {
71+
arr.push( new Complex64( i, i ) );
72+
}
73+
arr = new Complex64Array( arr );
74+
N = arr.length;
75+
76+
out = [ 0.0, 0.0 ];
77+
78+
b.tic();
79+
for ( i = 0; i < b.iterations; i++ ) {
80+
z = arr.get( out, i%N );
81+
if ( typeof z !== 'object' ) {
82+
b.fail( 'should return an object' );
83+
}
84+
}
85+
b.toc();
86+
if ( !isArray( z ) || z.length !== 2 ) {
87+
b.fail( 'should return an array' );
88+
}
89+
b.pass( 'benchmark finished' );
90+
b.end();
91+
});
92+
93+
bench( pkg+'::manual:get', function benchmark( b ) {
94+
var arr;
95+
var buf;
96+
var N;
97+
var z;
98+
var i;
99+
var j;
100+
101+
arr = [];
102+
for ( i = 0; i < 10; i++ ) {
103+
arr.push( new Complex64( i, i ) );
104+
}
105+
arr = new Complex64Array( arr );
106+
N = arr.length;
107+
108+
buf = new Float32Array( arr.buffer );
109+
110+
b.tic();
111+
for ( i = 0; i < b.iterations; i++ ) {
112+
j = ( i%N ) * 2;
113+
z = [ buf[ j ], buf[ j+1 ] ];
114+
if ( typeof z !== 'object' ) {
115+
b.fail( 'should return an object' );
116+
}
117+
}
118+
b.toc();
119+
if ( !isArray( z ) || z.length !== 2 ) {
120+
b.fail( 'should return an array' );
121+
}
122+
b.pass( 'benchmark finished' );
123+
b.end();
124+
});
125+
126+
bench( pkg+'::manual,memory_reuse:get', function benchmark( b ) {
127+
var arr;
128+
var buf;
129+
var out;
130+
var N;
131+
var i;
132+
var j;
133+
134+
arr = [];
135+
for ( i = 0; i < 10; i++ ) {
136+
arr.push( new Complex64( i, i ) );
137+
}
138+
arr = new Complex64Array( arr );
139+
N = arr.length;
140+
141+
buf = new Float32Array( arr.buffer );
142+
out = [ 0.0, 0.0 ];
143+
144+
b.tic();
145+
for ( i = 0; i < b.iterations; i++ ) {
146+
j = ( i%N ) * 2;
147+
out[ 0 ] = buf[ j ];
148+
out[ 1 ] = buf[ j+1 ];
149+
if ( typeof out !== 'object' ) {
150+
b.fail( 'should return an object' );
151+
}
152+
}
153+
b.toc();
154+
if ( !isArray( out ) || out.length !== 2 ) {
155+
b.fail( 'should return an array' );
156+
}
157+
b.pass( 'benchmark finished' );
158+
b.end();
159+
});

0 commit comments

Comments
 (0)