Skip to content

Commit 5af94f6

Browse files
committed
Add set benchmarks
1 parent 3023c94 commit 5af94f6

File tree

18 files changed

+1872
-0
lines changed

18 files changed

+1872
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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 pkg = require( './../package.json' ).name;
25+
var Float32Array = require( './../lib' );
26+
27+
28+
// MAIN //
29+
30+
bench( pkg+'::array:set', function benchmark( b ) {
31+
var values;
32+
var buf;
33+
var arr;
34+
var N;
35+
var v;
36+
var i;
37+
38+
values = [];
39+
for ( i = 0; i < 10; i++ ) {
40+
values.push( i );
41+
}
42+
N = values.length;
43+
44+
arr = new Float32Array( 2 );
45+
buf = [ 0.0 ];
46+
47+
b.tic();
48+
for ( i = 0; i < b.iterations; i++ ) {
49+
buf[ 0 ] = values[ i%N ];
50+
v = arr.set( buf );
51+
if ( typeof v !== 'undefined' ) {
52+
b.fail( 'should return undefined' );
53+
}
54+
}
55+
b.toc();
56+
if ( typeof v !== 'undefined' ) {
57+
b.fail( 'should return undefined' );
58+
}
59+
b.pass( 'benchmark finished' );
60+
b.end();
61+
});
62+
63+
bench( pkg+'::typed_array:set', function benchmark( b ) {
64+
var values;
65+
var buf;
66+
var arr;
67+
var N;
68+
var v;
69+
var i;
70+
71+
values = new Float32Array( 20 );
72+
N = values.length;
73+
for ( i = 0; i < N; i++ ) {
74+
values[ i ] = i;
75+
}
76+
77+
arr = new Float32Array( 2 );
78+
buf = new Float32Array( 1 );
79+
80+
b.tic();
81+
for ( i = 0; i < b.iterations; i++ ) {
82+
buf[ 0 ] = values[ i%N ];
83+
v = arr.set( buf );
84+
if ( typeof v !== 'undefined' ) {
85+
b.fail( 'should return undefined' );
86+
}
87+
}
88+
b.toc();
89+
if ( typeof v !== 'undefined' ) {
90+
b.fail( 'should return undefined' );
91+
}
92+
b.pass( 'benchmark finished' );
93+
b.end();
94+
});
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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 pow = require( '@stdlib/math/base/special/pow' );
25+
var randi = require( '@stdlib/random/base/randi' );
26+
var pkg = require( './../package.json' ).name;
27+
var Float32Array = require( './../lib' );
28+
29+
30+
// FUNCTIONS //
31+
32+
/**
33+
* Creates a benchmark function.
34+
*
35+
* @private
36+
* @param {PositiveInteger} len - array length
37+
* @returns {Function} benchmark function
38+
*/
39+
function createBenchmark( len ) {
40+
var values;
41+
var arr1;
42+
var arr2;
43+
var arr;
44+
var N;
45+
var i;
46+
47+
arr1 = [];
48+
arr2 = [];
49+
for ( i = 0; i < len; i++ ) {
50+
arr1.push( randi() );
51+
arr2.push( randi() );
52+
}
53+
arr = new Float32Array( len );
54+
55+
values = [
56+
arr1,
57+
arr2
58+
];
59+
N = values.length;
60+
61+
return benchmark;
62+
63+
/**
64+
* Benchmark function.
65+
*
66+
* @private
67+
* @param {Benchmark} b - benchmark instance
68+
*/
69+
function benchmark( b ) {
70+
var v;
71+
var i;
72+
73+
b.tic();
74+
for ( i = 0; i < b.iterations; i++ ) {
75+
v = arr.set( values[ i%N ] );
76+
if ( typeof v !== 'undefined' ) {
77+
b.fail( 'should return undefined' );
78+
}
79+
}
80+
b.toc();
81+
if ( typeof v !== 'undefined' ) {
82+
b.fail( 'should return undefined' );
83+
}
84+
b.pass( 'benchmark finished' );
85+
b.end();
86+
}
87+
}
88+
89+
90+
// MAIN //
91+
92+
/**
93+
* Main execution sequence.
94+
*
95+
* @private
96+
*/
97+
function main() {
98+
var len;
99+
var min;
100+
var max;
101+
var f;
102+
var i;
103+
104+
min = 1; // 10^min
105+
max = 6; // 10^max
106+
107+
for ( i = min; i <= max; i++ ) {
108+
len = pow( 10, i );
109+
f = createBenchmark( len );
110+
bench( pkg+':set:len='+len, f );
111+
}
112+
}
113+
114+
main();
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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 pkg = require( './../package.json' ).name;
25+
var Float64Array = require( './../lib' );
26+
27+
28+
// MAIN //
29+
30+
bench( pkg+'::array:set', function benchmark( b ) {
31+
var values;
32+
var buf;
33+
var arr;
34+
var N;
35+
var v;
36+
var i;
37+
38+
values = [];
39+
for ( i = 0; i < 10; i++ ) {
40+
values.push( i );
41+
}
42+
N = values.length;
43+
44+
arr = new Float64Array( 2 );
45+
buf = [ 0.0 ];
46+
47+
b.tic();
48+
for ( i = 0; i < b.iterations; i++ ) {
49+
buf[ 0 ] = values[ i%N ];
50+
v = arr.set( buf );
51+
if ( typeof v !== 'undefined' ) {
52+
b.fail( 'should return undefined' );
53+
}
54+
}
55+
b.toc();
56+
if ( typeof v !== 'undefined' ) {
57+
b.fail( 'should return undefined' );
58+
}
59+
b.pass( 'benchmark finished' );
60+
b.end();
61+
});
62+
63+
bench( pkg+'::typed_array:set', function benchmark( b ) {
64+
var values;
65+
var buf;
66+
var arr;
67+
var N;
68+
var v;
69+
var i;
70+
71+
values = new Float64Array( 20 );
72+
N = values.length;
73+
for ( i = 0; i < N; i++ ) {
74+
values[ i ] = i;
75+
}
76+
77+
arr = new Float64Array( 2 );
78+
buf = new Float64Array( 1 );
79+
80+
b.tic();
81+
for ( i = 0; i < b.iterations; i++ ) {
82+
buf[ 0 ] = values[ i%N ];
83+
v = arr.set( buf );
84+
if ( typeof v !== 'undefined' ) {
85+
b.fail( 'should return undefined' );
86+
}
87+
}
88+
b.toc();
89+
if ( typeof v !== 'undefined' ) {
90+
b.fail( 'should return undefined' );
91+
}
92+
b.pass( 'benchmark finished' );
93+
b.end();
94+
});

0 commit comments

Comments
 (0)