Skip to content

Commit 5d6f297

Browse files
committed
Add sort benchmarks
1 parent 8f24ae3 commit 5d6f297

File tree

18 files changed

+1422
-0
lines changed

18 files changed

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

0 commit comments

Comments
 (0)