Skip to content

Commit 9431a09

Browse files
committed
Add benchmark
1 parent 4cb7822 commit 9431a09

File tree

1 file changed

+154
-0
lines changed
  • lib/node_modules/@stdlib/random/shuffle/benchmark

1 file changed

+154
-0
lines changed
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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 randu = require( '@stdlib/random/base/randu' );
25+
var floor = require( '@stdlib/math/base/special/floor' );
26+
var isArray = require( '@stdlib/assert/is-array' );
27+
var pkg = require( './../package.json' ).name;
28+
var shuffle = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg, function benchmark( b ) {
34+
var result;
35+
var arr;
36+
var len;
37+
var i;
38+
39+
arr = new Array( 100 );
40+
len = arr.length;
41+
for ( i = 0; i < len; i++ ) {
42+
arr[ i ] = floor( randu()*100.0 );
43+
}
44+
45+
b.tic();
46+
for ( i = 0; i < b.iterations; i++ ) {
47+
result = shuffle( arr );
48+
if ( !isArray( result ) ) {
49+
b.fail( 'should return an array' );
50+
}
51+
}
52+
b.toc();
53+
if ( !isArray( result ) ) {
54+
b.fail( 'should return an array' );
55+
}
56+
b.pass( 'benchmark finished' );
57+
b.end();
58+
});
59+
60+
bench( pkg+'::deep', function benchmark( b ) {
61+
var result;
62+
var arr;
63+
var len;
64+
var i;
65+
66+
arr = new Array( 100 );
67+
len = arr.length;
68+
for ( i = 0; i < len; i++ ) {
69+
arr[ i ] = {
70+
'beep': 'boop'
71+
};
72+
}
73+
74+
b.tic();
75+
for ( i = 0; i < b.iterations; i++ ) {
76+
result = shuffle( arr, {
77+
'copy': 'deep'
78+
});
79+
if ( !isArray( result ) ) {
80+
b.fail( 'should return an array' );
81+
}
82+
}
83+
b.toc();
84+
if ( !isArray( result ) ) {
85+
b.fail( 'should return an array' );
86+
}
87+
b.pass( 'benchmark finished' );
88+
b.end();
89+
});
90+
91+
bench( pkg+'::none', function benchmark( b ) {
92+
var result;
93+
var arr;
94+
var len;
95+
var i;
96+
97+
arr = new Array( 100 );
98+
len = arr.length;
99+
for ( i = 0; i < len; i++ ) {
100+
arr[ i ] = floor( randu()*100.0 );
101+
}
102+
103+
b.tic();
104+
for ( i = 0; i < b.iterations; i++ ) {
105+
result = shuffle( arr, {
106+
'copy': 'none'
107+
});
108+
if ( !isArray( result ) ) {
109+
b.fail( 'should return an array' );
110+
}
111+
if ( result !== arr ) {
112+
b.fail( 'should mutate the input array' );
113+
}
114+
}
115+
b.toc();
116+
if ( !isArray( result ) ) {
117+
b.fail( 'should return an array' );
118+
}
119+
if ( result !== arr ) {
120+
b.fail( 'should mutate the input array' );
121+
}
122+
b.pass( 'benchmark finished' );
123+
b.end();
124+
});
125+
126+
bench( pkg+':factory', function benchmark( b ) {
127+
var myshuffle;
128+
var result;
129+
var arr;
130+
var len;
131+
var i;
132+
133+
myshuffle = shuffle.factory();
134+
135+
arr = new Array( 100 );
136+
len = arr.length;
137+
for ( i = 0; i < len; i++ ) {
138+
arr[ i ] = floor( randu()*100.0 );
139+
}
140+
141+
b.tic();
142+
for ( i = 0; i < b.iterations; i++ ) {
143+
result = myshuffle( arr );
144+
if ( !isArray( result ) ) {
145+
b.fail( 'should return an array' );
146+
}
147+
}
148+
b.toc();
149+
if ( !isArray( result ) ) {
150+
b.fail( 'should return an array' );
151+
}
152+
b.pass( 'benchmark finished' );
153+
b.end();
154+
});

0 commit comments

Comments
 (0)