Skip to content

Commit f6cca6b

Browse files
gururaj1512kgryte
andauthored
bench: refactor benchmarks in math/base/special/rsqrtf
PR-URL: #5223 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent 8449e0b commit f6cca6b

File tree

6 files changed

+51
-37
lines changed

6 files changed

+51
-37
lines changed

lib/node_modules/@stdlib/math/base/special/rsqrtf/benchmark/benchmark.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2020 The Stdlib Authors.
4+
* Copyright (c) 2025 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -21,7 +21,7 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
24+
var randu = require( '@stdlib/random/array/uniform' );
2525
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2626
var pkg = require( './../package.json' ).name;
2727
var rsqrtf = require( './../lib' );
@@ -34,10 +34,13 @@ bench( pkg, function benchmark( b ) {
3434
var y;
3535
var i;
3636

37+
x = randu( 100, 0.0, 100000.0, {
38+
'dtype': 'float32'
39+
});
40+
3741
b.tic();
3842
for ( i = 0; i < b.iterations; i++ ) {
39-
x = ( randu()*100000.0 ) - 0.0;
40-
y = rsqrtf( x );
43+
y = rsqrtf( x[ i % x.length ] );
4144
if ( isnanf( y ) ) {
4245
b.fail( 'should not return NaN' );
4346
}
@@ -55,10 +58,13 @@ bench( pkg+'::built-in', function benchmark( b ) {
5558
var y;
5659
var i;
5760

61+
x = randu( 100, 0.0, 100000.0, {
62+
'dtype': 'float32'
63+
});
64+
5865
b.tic();
5966
for ( i = 0; i < b.iterations; i++ ) {
60-
x = ( randu()*100000.0 ) - 0.0;
61-
y = 1.0 / Math.sqrt( x ); // eslint-disable-line stdlib/no-builtin-math
67+
y = 1.0 / Math.sqrt( x[ i % x.length ] ); // eslint-disable-line stdlib/no-builtin-math
6268
if ( isnanf( y ) ) {
6369
b.fail( 'should not return NaN' );
6470
}

lib/node_modules/@stdlib/math/base/special/rsqrtf/benchmark/benchmark.native.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2020 The Stdlib Authors.
4+
* Copyright (c) 2025 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -22,7 +22,7 @@
2222

2323
var resolve = require( 'path' ).resolve;
2424
var bench = require( '@stdlib/bench' );
25-
var randu = require( '@stdlib/random/base/randu' );
25+
var randu = require( '@stdlib/random/array/uniform' );
2626
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2727
var tryRequire = require( '@stdlib/utils/try-require' );
2828
var pkg = require( './../package.json' ).name;
@@ -43,10 +43,13 @@ bench( pkg+'::native', opts, function benchmark( b ) {
4343
var y;
4444
var i;
4545

46+
x = randu( 100, 0.0, 100000.0, {
47+
'dtype': 'float32'
48+
});
49+
4650
b.tic();
4751
for ( i = 0; i < b.iterations; i++ ) {
48-
x = ( randu()*100000.0 ) - 0.0;
49-
y = rsqrtf( x );
52+
y = rsqrtf( x[ i % x.length ] );
5053
if ( isnanf( y ) ) {
5154
b.fail( 'should not return NaN' );
5255
}

lib/node_modules/@stdlib/math/base/special/rsqrtf/benchmark/c/Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#/
22
# @license Apache-2.0
33
#
4-
# Copyright (c) 2020 The Stdlib Authors.
4+
# Copyright (c) 2025 The Stdlib Authors.
55
#
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.
@@ -16,7 +16,6 @@
1616
# limitations under the License.
1717
#/
1818

19-
2019
# VARIABLES #
2120

2221
ifndef VERBOSE

lib/node_modules/@stdlib/math/base/special/rsqrtf/benchmark/c/benchmark.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2020 The Stdlib Authors.
4+
* Copyright (c) 2025 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -90,15 +90,18 @@ static float rand_float( void ) {
9090
*/
9191
static double benchmark( void ) {
9292
double elapsed;
93-
float x;
94-
float y;
93+
float x[ 100 ];
9594
double t;
95+
float y;
9696
int i;
9797

98+
for ( i = 0; i < 100; i++ ) {
99+
x[ i ] = 100000.0f * rand_float();
100+
}
101+
98102
t = tic();
99103
for ( i = 0; i < ITERATIONS; i++ ) {
100-
x = rand_float() * 100000.0f;
101-
y = 1.0f / sqrtf( x );
104+
y = 1.0f / sqrtf( x[ i % 100 ] );
102105
if ( y != y ) {
103106
printf( "should not return NaN\n" );
104107
break;

lib/node_modules/@stdlib/math/base/special/rsqrtf/benchmark/c/native/Makefile

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#/
22
# @license Apache-2.0
33
#
4-
# Copyright (c) 2020 The Stdlib Authors.
4+
# Copyright (c) 2025 The Stdlib Authors.
55
#
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.
@@ -88,15 +88,15 @@ c_targets := benchmark.out
8888
# RULES #
8989

9090
#/
91-
# Compiles source files.
91+
# Compiles C source files.
9292
#
93-
# @param {string} [C_COMPILER] - C compiler (e.g., `gcc`)
94-
# @param {string} [CFLAGS] - C compiler options
95-
# @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`)
96-
# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop/include`)
97-
# @param {string} [SOURCE_FILES] - list of source files
93+
# @param {string} SOURCE_FILES - list of C source files
94+
# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`)
95+
# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`)
9896
# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`)
99-
# @param {string} [LIBRARIES] - list of libraries (e.g., `-lopenblas -lpthread`)
97+
# @param {string} [C_COMPILER] - C compiler
98+
# @param {string} [CFLAGS] - C compiler flags
99+
# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code
100100
#
101101
# @example
102102
# make
@@ -112,13 +112,13 @@ all: $(c_targets)
112112
# Compiles C source files.
113113
#
114114
# @private
115-
# @param {string} CC - C compiler (e.g., `gcc`)
116-
# @param {string} CFLAGS - C compiler options
117-
# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`)
118-
# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar`)
119-
# @param {string} SOURCE_FILES - list of source files
120-
# @param {string} LIBPATH - list of library paths (e.g., `-L /foo/bar`)
121-
# @param {string} LIBRARIES - list of libraries (e.g., `-lopenblas`)
115+
# @param {string} SOURCE_FILES - list of C source files
116+
# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`)
117+
# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`)
118+
# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`)
119+
# @param {string} CC - C compiler
120+
# @param {string} CFLAGS - C compiler flags
121+
# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code
122122
#/
123123
$(c_targets): %.out: %.c
124124
$(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES)

lib/node_modules/@stdlib/math/base/special/rsqrtf/benchmark/c/native/benchmark.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2020 The Stdlib Authors.
4+
* Copyright (c) 2025 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -91,15 +91,18 @@ static float rand_float( void ) {
9191
*/
9292
static double benchmark( void ) {
9393
double elapsed;
94-
float x;
95-
float y;
94+
float x[ 100 ];
9695
double t;
96+
float y;
9797
int i;
9898

99+
for ( i = 0; i < 100; i++ ) {
100+
x[ i ] = 100000.0f * rand_float();
101+
}
102+
99103
t = tic();
100104
for ( i = 0; i < ITERATIONS; i++ ) {
101-
x = rand_float() * 100000.0f;
102-
y = stdlib_base_rsqrtf( x );
105+
y = stdlib_base_rsqrtf( x[ i % 100 ] );
103106
if ( y != y ) {
104107
printf( "should not return NaN\n" );
105108
break;

0 commit comments

Comments
 (0)