Skip to content

Commit e45a3aa

Browse files
bench: add JS benchmarks for `ml/strided/dkmeans-init-plus-plus
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown_pkg_readmes status: na - task: lint_markdown_docs status: na - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 4d8fd87 commit e45a3aa

2 files changed

Lines changed: 306 additions & 0 deletions

File tree

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 uniform = require( '@stdlib/random/array/uniform' );
25+
var Float64Array = require( '@stdlib/array/float64' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var pow = require( '@stdlib/math/base/special/pow' );
28+
var format = require( '@stdlib/string/format' );
29+
var pkg = require( './../package.json' ).name;
30+
var dkmeansInitPlusPlus = require( './../lib/dkmeans_init_plus_plus.js' );
31+
32+
33+
// VARIABLES //
34+
35+
var LAYOUTS = [
36+
'row-major',
37+
'column-major'
38+
];
39+
40+
var options = {
41+
'dtype': 'float64'
42+
};
43+
44+
45+
// FUNCTIONS //
46+
47+
/**
48+
* Creates a benchmark function.
49+
*
50+
* @private
51+
* @param {string} order - storage layout
52+
* @param {PositiveInteger} k - number of clusters
53+
* @param {PositiveInteger} M - number of data points
54+
* @param {PositiveInteger} N - number of features
55+
* @param {string} metric - distance metric
56+
* @param {PositiveInteger} trials - number of potential centroids per iteration (>= 1)
57+
* @returns {Function} benchmark function
58+
*/
59+
function createBenchmark( order, k, M, N, metric, trials ) {
60+
var out = new Float64Array( k*N );
61+
var x = uniform( M*N, -100.0, 100.0, options );
62+
return benchmark;
63+
64+
/**
65+
* Benchmark function.
66+
*
67+
* @private
68+
* @param {Benchmark} b - benchmark instance
69+
*/
70+
function benchmark( b ) {
71+
var c;
72+
var i;
73+
74+
b.tic();
75+
for ( i = 0; i < b.iterations; i++ ) {
76+
c = dkmeansInitPlusPlus( order, k, M, N, out, N, x, N, metric, trials, 44 ); // eslint-disable-line max-len
77+
if ( isnan( c[ i%(k*N) ] ) ) {
78+
b.fail( 'should not return NaN' );
79+
}
80+
}
81+
b.toc();
82+
if ( isnan( c[ 0 ] ) ) {
83+
b.fail( 'should not return NaN' );
84+
}
85+
b.pass( 'benchmark finished' );
86+
b.end();
87+
}
88+
}
89+
90+
91+
// MAIN //
92+
93+
/**
94+
* Main execution sequence.
95+
*
96+
* @private
97+
*/
98+
function main() {
99+
var metrics;
100+
var trials;
101+
var min;
102+
var max;
103+
var M;
104+
var N;
105+
var k;
106+
var t;
107+
var m;
108+
var i;
109+
var j;
110+
var f;
111+
112+
min = 1; // 10^min
113+
max = 4; // 10^max
114+
115+
// Benchmark: vary order with defaults k=2, M=100, N=2, metric=sqeuclidean, trials=1...
116+
for ( i = 0; i <= LAYOUTS.length; i++ ) {
117+
f = createBenchmark( LAYOUTS[ i ], 2, 100, 2, 'sqeuclidean', 1 );
118+
bench( format( '%s::vary_M:order=%s,k=2,M=100,N=2,metric=sqeuclidean,trials=1', pkg, LAYOUTS[ i ] ), f );
119+
}
120+
121+
// Benchmark: vary number of data points (M) with defaults k=2, N=2, metric=sqeuclidean, trials=1...
122+
for ( i = min; i <= max; i++ ) {
123+
M = pow( 10, i );
124+
f = createBenchmark( 'row-major', 2, M, 2, 'sqeuclidean', 1 );
125+
bench( format( '%s::vary_M:order=row-major,k=2,M=%d,N=2,metric=sqeuclidean,trials=1', pkg, M ), f );
126+
}
127+
128+
// Benchmark: vary number of features (N) with defaults k=2, M=10, metric=sqeuclidean, trials=1...
129+
for ( i = min; i <= max; i++ ) {
130+
N = pow( 10, i );
131+
f = createBenchmark( 'row-major', 2, 10, N, 'sqeuclidean', 1 );
132+
bench( format( '%s::vary_N:order=row-major,k=2,M=1000,N=%d,metric=sqeuclidean,trials=1', pkg, N ), f );
133+
}
134+
135+
// Benchmark: vary number of clusters (k) with defaults M=1000, N=2, metric=sqeuclidean, trials=1...
136+
for ( i = 1; i <= 4; i++ ) {
137+
k = pow( 2, i );
138+
f = createBenchmark( 'row-major', k, 1000, 2, 'sqeuclidean', 1 );
139+
bench( format( '%s::vary_k:order=row-major,k=%d,M=1000,N=2,metric=sqeuclidean,trials=1', pkg, k ), f );
140+
}
141+
142+
// Benchmark: vary number of trials with defaults k=2, M=1000, N=2, metric=sqeuclidean...
143+
trials = [ 1, 10, 100, 1000 ];
144+
for ( j = 0; j < trials.length; j++ ) {
145+
t = trials[ j ];
146+
f = createBenchmark( 'row-major', 2, 1000, 2, 'sqeuclidean', t );
147+
bench( format( '%s::vary_trials:order=row-major,k=2,M=1000,N=2,metric=sqeuclidean,trials=%d', pkg, t ), f );
148+
}
149+
150+
// Benchmark: vary metric with defaults k=2, M=1000, N=2, trials=1...
151+
metrics = [ 'sqeuclidean', 'cosine', 'cityblock', 'correlation' ];
152+
for ( j = 0; j < metrics.length; j++ ) {
153+
m = metrics[ j ];
154+
f = createBenchmark( 'row-major', 2, 1000, 2, m, 1 );
155+
bench( format( '%s::vary_metric:order=row-major,k=2,M=1000,N=2,metric=%s,trials=1', pkg, m ), f );
156+
}
157+
}
158+
159+
main();
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 uniform = require( '@stdlib/random/array/uniform' );
25+
var Float64Array = require( '@stdlib/array/float64' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var pow = require( '@stdlib/math/base/special/pow' );
28+
var format = require( '@stdlib/string/format' );
29+
var pkg = require( './../package.json' ).name;
30+
var dkmeansInitPlusPlus = require( './../lib/ndarray.js' );
31+
32+
33+
// VARIABLES //
34+
35+
var options = {
36+
'dtype': 'float64'
37+
};
38+
39+
40+
// FUNCTIONS //
41+
42+
/**
43+
* Creates a benchmark function.
44+
*
45+
* @private
46+
* @param {PositiveInteger} k - number of clusters
47+
* @param {PositiveInteger} M - number of data points
48+
* @param {PositiveInteger} N - number of features
49+
* @param {string} metric - distance metric
50+
* @param {PositiveInteger} trials - number of potential centroids per iteration (>= 1)
51+
* @returns {Function} benchmark function
52+
*/
53+
function createBenchmark( k, M, N, metric, trials ) {
54+
var out = new Float64Array( k*N );
55+
var x = uniform( M*N, -100.0, 100.0, options );
56+
return benchmark;
57+
58+
/**
59+
* Benchmark function.
60+
*
61+
* @private
62+
* @param {Benchmark} b - benchmark instance
63+
*/
64+
function benchmark( b ) {
65+
var c;
66+
var i;
67+
68+
b.tic();
69+
for ( i = 0; i < b.iterations; i++ ) {
70+
c = dkmeansInitPlusPlus( k, M, N, out, N, 1, 0, x, N, 1, 0, metric, trials, 44 ); // eslint-disable-line max-len
71+
if ( isnan( c[ i%(k*N) ] ) ) {
72+
b.fail( 'should not return NaN' );
73+
}
74+
}
75+
b.toc();
76+
if ( isnan( c[ 0 ] ) ) {
77+
b.fail( 'should not return NaN' );
78+
}
79+
b.pass( 'benchmark finished' );
80+
b.end();
81+
}
82+
}
83+
84+
85+
// MAIN //
86+
87+
/**
88+
* Main execution sequence.
89+
*
90+
* @private
91+
*/
92+
function main() {
93+
var metrics;
94+
var trials;
95+
var min;
96+
var max;
97+
var M;
98+
var N;
99+
var k;
100+
var t;
101+
var m;
102+
var i;
103+
var j;
104+
var f;
105+
106+
min = 1; // 10^min
107+
max = 4; // 10^max
108+
109+
// Benchmark: vary number of data points (M) with defaults k=2, N=2, metric=sqeuclidean, trials=1...
110+
for ( i = min; i <= max; i++ ) {
111+
M = pow( 10, i );
112+
f = createBenchmark( 2, M, 2, 'sqeuclidean', 1 );
113+
bench( format( '%s::vary_M:k=2,M=%d,N=2,metric=sqeuclidean,trials=1', pkg, M ), f );
114+
}
115+
116+
// Benchmark: vary number of features (N) with defaults k=2, M=10, metric=sqeuclidean, trials=1...
117+
for ( i = min; i <= max; i++ ) {
118+
N = pow( 10, i );
119+
f = createBenchmark( 2, 10, N, 'sqeuclidean', 1 );
120+
bench( format( '%s::vary_N:k=2,M=1000,N=%d,metric=sqeuclidean,trials=1', pkg, N ), f );
121+
}
122+
123+
// Benchmark: vary number of clusters (k) with defaults M=1000, N=2, metric=sqeuclidean, trials=1...
124+
for ( i = 1; i <= 4; i++ ) {
125+
k = pow( 2, i );
126+
f = createBenchmark( k, 1000, 2, 'sqeuclidean', 1 );
127+
bench( format( '%s::vary_k:k=%d,M=1000,N=2,metric=sqeuclidean,trials=1', pkg, k ), f );
128+
}
129+
130+
// Benchmark: vary number of trials with defaults k=2, M=1000, N=2, metric=sqeuclidean...
131+
trials = [ 1, 10, 100 ];
132+
for ( j = 0; j < trials.length; j++ ) {
133+
t = trials[ j ];
134+
f = createBenchmark( 2, 1000, 2, 'sqeuclidean', t );
135+
bench( format( '%s::vary_trials:k=2,M=1000,N=2,metric=sqeuclidean,trials=%d', pkg, t ), f );
136+
}
137+
138+
// Benchmark: vary metric with defaults k=2, M=1000, N=2, trials=1...
139+
metrics = [ 'sqeuclidean', 'cosine', 'cityblock', 'correlation' ];
140+
for ( j = 0; j < metrics.length; j++ ) {
141+
m = metrics[ j ];
142+
f = createBenchmark( 2, 1000, 2, m, 1 );
143+
bench( format( '%s::vary_metric:k=2,M=1000,N=2,metric=%s,trials=1', pkg, m ), f );
144+
}
145+
}
146+
147+
main();

0 commit comments

Comments
 (0)