Skip to content

Commit 27720a2

Browse files
committed
Add benchmarks for ndarray instantiation
1 parent 84d809c commit 27720a2

1 file changed

Lines changed: 100 additions & 0 deletions

File tree

  • lib/node_modules/@stdlib/types/ndarray/ctor/benchmark
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
'use strict';
2+
3+
// MODULES //
4+
5+
var bench = require( '@stdlib/bench' );
6+
var isFunction = require( '@stdlib/assert/is-function' );
7+
var randu = require( '@stdlib/math/base/random/randu' );
8+
var floor = require( '@stdlib/math/base/special/floor' );
9+
var pkg = require( './../package.json' ).name;
10+
var ctor = require( './../lib' );
11+
12+
13+
// MAIN //
14+
15+
bench( pkg, function benchmark( b ) {
16+
var out;
17+
var i;
18+
19+
b.tic();
20+
for ( i = 0; i < b.iterations; i++ ) {
21+
out = ctor( 'float64', 1.0+floor( randu()*10.0 ) );
22+
if ( typeof out !== 'function' ) {
23+
b.fail( 'should return a function' );
24+
}
25+
}
26+
b.toc();
27+
if ( !isFunction( out ) ) {
28+
b.fail( 'should return a function' );
29+
}
30+
b.pass( 'benchmark finished' );
31+
b.end();
32+
});
33+
34+
bench( pkg+'::instantiation', function benchmark( b ) {
35+
var ndarray;
36+
var strides;
37+
var buffer;
38+
var offset;
39+
var shape;
40+
var order;
41+
var out;
42+
var i;
43+
44+
buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ];
45+
shape = [ 3, 2 ];
46+
strides = [ 2, 1 ];
47+
offset = 0;
48+
order = 'row-major';
49+
50+
ndarray = ctor( 'float64', 2 );
51+
52+
b.tic();
53+
for ( i = 0; i < b.iterations; i++ ) {
54+
buffer[ 0 ] = randu();
55+
out = ndarray( buffer, shape, strides, offset, order );
56+
if ( out.length === 0 ) {
57+
b.fail( 'should have a length greater than 0' );
58+
}
59+
}
60+
b.toc();
61+
if ( out.length === 0 ) {
62+
b.fail( 'should have a length greater than 0' );
63+
}
64+
b.pass( 'benchmark finished' );
65+
b.end();
66+
});
67+
68+
bench( pkg+'::instantiation,new', function benchmark( b ) {
69+
var ndarray;
70+
var strides;
71+
var buffer;
72+
var offset;
73+
var shape;
74+
var order;
75+
var out;
76+
var i;
77+
78+
buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ];
79+
shape = [ 3, 2 ];
80+
strides = [ 2, 1 ];
81+
offset = 0;
82+
order = 'row-major';
83+
84+
ndarray = ctor( 'float64', 2 );
85+
86+
b.tic();
87+
for ( i = 0; i < b.iterations; i++ ) {
88+
buffer[ 0 ] = randu();
89+
out = new ndarray( buffer, shape, strides, offset, order ); // eslint-disable-line no-new-cap
90+
if ( out.length === 0 ) {
91+
b.fail( 'should have a length greater than 0' );
92+
}
93+
}
94+
b.toc();
95+
if ( out.length === 0 ) {
96+
b.fail( 'should have a length greater than 0' );
97+
}
98+
b.pass( 'benchmark finished' );
99+
b.end();
100+
});

0 commit comments

Comments
 (0)