Skip to content

Commit fd45831

Browse files
gururaj1512kgryte
andauthored
bench: refactor benchmarks and update test messages in math/base/special/round
PR-URL: #4950 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent 99b9273 commit fd45831

File tree

10 files changed

+123
-91
lines changed

10 files changed

+123
-91
lines changed

Diff for: lib/node_modules/@stdlib/math/base/special/round/benchmark/benchmark.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2018 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 isnan = require( '@stdlib/math/base/assert/is-nan' );
2626
var pkg = require( './../package.json' ).name;
2727
var round = require( './../lib' );
@@ -34,10 +34,11 @@ bench( pkg, function benchmark( b ) {
3434
var y;
3535
var i;
3636

37+
x = randu( 100, -500.0, 500.0 );
38+
3739
b.tic();
3840
for ( i = 0; i < b.iterations; i++ ) {
39-
x = ( randu()*1000.0 ) - 500.0;
40-
y = round( x );
41+
y = round( x[ i % x.length ] );
4142
if ( isnan( y ) ) {
4243
b.fail( 'should not return NaN' );
4344
}
@@ -55,10 +56,11 @@ bench( pkg+'::built-in', function benchmark( b ) {
5556
var y;
5657
var i;
5758

59+
x = randu( 100, -500.0, 500.0 );
60+
5861
b.tic();
5962
for ( i = 0; i < b.iterations; i++ ) {
60-
x = ( randu()*1000.0 ) - 500.0;
61-
y = Math.round( x ); // eslint-disable-line stdlib/no-builtin-math
63+
y = Math.round( x[ i % x.length ] ); // eslint-disable-line stdlib/no-builtin-math
6264
if ( isnan( y ) ) {
6365
b.fail( 'should not return NaN' );
6466
}

Diff for: lib/node_modules/@stdlib/math/base/special/round/benchmark/benchmark.native.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2024 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 isnan = require( '@stdlib/math/base/assert/is-nan' );
2727
var tryRequire = require( '@stdlib/utils/try-require' );
2828
var pkg = require( './../package.json' ).name;
@@ -43,10 +43,11 @@ bench( pkg+'::native', opts, function benchmark( b ) {
4343
var y;
4444
var i;
4545

46+
x = randu( 100, -500.0, 500.0 );
47+
4648
b.tic();
4749
for ( i = 0; i < b.iterations; i++ ) {
48-
x = ( randu()*1000.0 ) - 500.0;
49-
y = round( x );
50+
y = round( x[ i % x.length ] );
5051
if ( isnan( y ) ) {
5152
b.fail( 'should not return NaN' );
5253
}

Diff for: lib/node_modules/@stdlib/math/base/special/round/benchmark/c/Makefile

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#/
22
# @license Apache-2.0
33
#
4-
# Copyright (c) 2018 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

Diff for: lib/node_modules/@stdlib/math/base/special/round/benchmark/c/benchmark.c

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2018 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.
@@ -89,16 +89,19 @@ static double rand_double( void ) {
8989
* @return elapsed time in seconds
9090
*/
9191
static double benchmark( void ) {
92+
double x[ 100 ];
9293
double elapsed;
93-
double x;
9494
double y;
9595
double t;
9696
int i;
9797

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

Diff for: lib/node_modules/@stdlib/math/base/special/round/benchmark/c/cephes/Makefile

+41-20
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#/
22
# @license Apache-2.0
33
#
4-
# Copyright (c) 2018 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,11 +16,12 @@
1616
# limitations under the License.
1717
#/
1818

19-
2019
# VARIABLES #
2120

2221
ifndef VERBOSE
2322
QUIET := @
23+
else
24+
QUIET :=
2425
endif
2526

2627
# Specify the path to Cephes:
@@ -29,7 +30,7 @@ CEPHES ?=
2930
# Specify a list of Cephes source files:
3031
CEPHES_SRC ?=
3132

32-
# Determine the OS:
33+
# Determine the OS ([1][1], [2][2]).
3334
#
3435
# [1]: https://en.wikipedia.org/wiki/Uname#Examples
3536
# [2]: http://stackoverflow.com/a/27776822/2225624
@@ -42,6 +43,10 @@ ifneq (, $(findstring MSYS,$(OS)))
4243
else
4344
ifneq (, $(findstring CYGWIN,$(OS)))
4445
OS := WINNT
46+
else
47+
ifneq (, $(findstring Windows_NT,$(OS)))
48+
OS := WINNT
49+
endif
4550
endif
4651
endif
4752
endif
@@ -60,7 +65,7 @@ CFLAGS ?= \
6065
-Wall \
6166
-pedantic
6267

63-
# Determine whether to generate [position independent code][1]:
68+
# Determine whether to generate position independent code ([1][1], [2][2]).
6469
#
6570
# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options
6671
# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option
@@ -74,39 +79,55 @@ endif
7479
c_targets := benchmark.out
7580

7681

77-
# TARGETS #
82+
# RULES #
7883

79-
# Default target.
84+
#/
85+
# Compiles C source files.
8086
#
81-
# This target is the default target.
82-
87+
# @param {string} CEPHES_SRC - list of Cephes source files
88+
# @param {string} [C_COMPILER] - C compiler
89+
# @param {string} [CFLAGS] - C compiler flags
90+
# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code
91+
#
92+
# @example
93+
# make
94+
#
95+
# @example
96+
# make all
97+
#/
8398
all: $(c_targets)
8499

85100
.PHONY: all
86101

87-
88-
# Compile C source.
102+
#/
103+
# Compiles C source files.
89104
#
90-
# This target compiles C source files.
91-
105+
# @private
106+
# @param {string} CC - C compiler
107+
# @param {string} CFLAGS - C compiler flags
108+
# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code
109+
# @param {string} CEPHES_SRC - list of Cephes source files
110+
#/
92111
$(c_targets): %.out: %.c
93112
$(QUIET) $(CC) $(CFLAGS) $(fPIC) -o $@ $(CEPHES_SRC) $< -lm
94113

95-
96-
# Run a benchmark.
114+
#/
115+
# Runs compiled benchmarks.
97116
#
98-
# This target runs a benchmark.
99-
117+
# @example
118+
# make run
119+
#/
100120
run: $(c_targets)
101121
$(QUIET) ./$<
102122

103123
.PHONY: run
104124

105-
106-
# Perform clean-up.
125+
#/
126+
# Removes generated files.
107127
#
108-
# This target removes generated files.
109-
128+
# @example
129+
# make clean
130+
#/
110131
clean:
111132
$(QUIET) -rm -f *.o *.out
112133

Diff for: lib/node_modules/@stdlib/math/base/special/round/benchmark/c/cephes/benchmark.c

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2018 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.
@@ -94,16 +94,19 @@ static double rand_double( void ) {
9494
* @return elapsed time in seconds
9595
*/
9696
static double benchmark( void ) {
97+
double x[ 100 ];
9798
double elapsed;
98-
double x;
9999
double y;
100100
double t;
101101
int i;
102102

103+
for ( i = 0; i < 100; i++ ) {
104+
x[ i ] = ( 1000.0 * rand_double() ) - 500.0;
105+
}
106+
103107
t = tic();
104108
for ( i = 0; i < ITERATIONS; i++ ) {
105-
x = ( 1000.0*rand_double() ) - 500.0;
106-
y = round( x );
109+
y = round( x[ i % 100 ] );
107110
if ( y != y ) {
108111
printf( "should not return NaN\n" );
109112
break;

Diff for: lib/node_modules/@stdlib/math/base/special/round/benchmark/c/native/Makefile

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#/
22
# @license Apache-2.0
33
#
4-
# Copyright (c) 2024 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)

Diff for: lib/node_modules/@stdlib/math/base/special/round/benchmark/c/native/benchmark.c

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2024 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,16 +90,19 @@ static double rand_double( void ) {
9090
* @return elapsed time in seconds
9191
*/
9292
static double benchmark( void ) {
93+
double x[ 100 ];
9394
double elapsed;
94-
double x;
9595
double y;
9696
double t;
9797
int i;
9898

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

0 commit comments

Comments
 (0)