From e9869d35ae9211fa93fdbaafe499a9e17f467b8c Mon Sep 17 00:00:00 2001 From: hrshya Date: Tue, 15 Apr 2025 15:07:34 +0530 Subject: [PATCH 1/2] bench: update random value generation --- .../is-safe-integer/benchmark/benchmark.js | 12 ++++++++---- .../benchmark/benchmark.native.js | 12 ++++++++---- .../benchmark/c/native/benchmark.c | 9 ++++++--- .../base/assert/is-safe-integer/test/test.js | 18 +++++++++--------- .../assert/is-safe-integer/test/test.native.js | 18 +++++++++--------- .../uint32-is-pow2/benchmark/benchmark.js | 12 ++++++++---- .../benchmark/benchmark.native.js | 12 ++++++++---- .../benchmark/c/native/benchmark.c | 9 ++++++--- .../base/assert/uint32-is-pow2/test/test.js | 2 +- .../assert/uint32-is-pow2/test/test.native.js | 2 +- 10 files changed, 64 insertions(+), 42 deletions(-) diff --git a/lib/node_modules/@stdlib/math/base/assert/is-safe-integer/benchmark/benchmark.js b/lib/node_modules/@stdlib/math/base/assert/is-safe-integer/benchmark/benchmark.js index 09c7bbaf6d89..ada66a7e209d 100644 --- a/lib/node_modules/@stdlib/math/base/assert/is-safe-integer/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/math/base/assert/is-safe-integer/benchmark/benchmark.js @@ -21,8 +21,7 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var randu = require( '@stdlib/random/base/randu' ); -var round = require( '@stdlib/math/base/special/round' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; var pkg = require( './../package.json' ).name; var isSafeInteger = require( './../lib' ); @@ -31,14 +30,19 @@ var isSafeInteger = require( './../lib' ); // MAIN // bench( pkg, function benchmark( b ) { + var opts; var x; var y; var i; + opts = { + 'dtype': 'float64' + }; + x = discreteUniform( 100, -5.0e6, 5.0e6, opts ); + b.tic(); for ( i = 0; i < b.iterations; i++ ) { - x = round( (randu()*1.0e7) - 5.0e6 ); - y = isSafeInteger( x ); + y = isSafeInteger( x[ i%x.length ] ); if ( typeof y !== 'boolean' ) { b.fail( 'should return a boolean' ); } diff --git a/lib/node_modules/@stdlib/math/base/assert/is-safe-integer/benchmark/benchmark.native.js b/lib/node_modules/@stdlib/math/base/assert/is-safe-integer/benchmark/benchmark.native.js index 3e544c9db4c1..feca7bd81449 100644 --- a/lib/node_modules/@stdlib/math/base/assert/is-safe-integer/benchmark/benchmark.native.js +++ b/lib/node_modules/@stdlib/math/base/assert/is-safe-integer/benchmark/benchmark.native.js @@ -22,8 +22,7 @@ var resolve = require( 'path' ).resolve; var bench = require( '@stdlib/bench' ); -var randu = require( '@stdlib/random/base/randu' ); -var round = require( '@stdlib/math/base/special/round' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; var tryRequire = require( '@stdlib/utils/try-require' ); var pkg = require( './../package.json' ).name; @@ -40,14 +39,19 @@ var opts = { // MAIN // bench( pkg+'::native', opts, function benchmark( b ) { + var opts; var x; var y; var i; + opts = { + 'dtype': 'float64' + }; + x = discreteUniform( 100, -5.0e6, 5.0e6, opts ); + b.tic(); for ( i = 0; i < b.iterations; i++ ) { - x = round( (randu()*1.0e7) - 5.0e6 ); - y = isSafeInteger( x ); + y = isSafeInteger( x[ i%x.length ] ); if ( typeof y !== 'boolean' ) { b.fail( 'should return a boolean' ); } diff --git a/lib/node_modules/@stdlib/math/base/assert/is-safe-integer/benchmark/c/native/benchmark.c b/lib/node_modules/@stdlib/math/base/assert/is-safe-integer/benchmark/c/native/benchmark.c index 902f642eed11..6e9637721882 100644 --- a/lib/node_modules/@stdlib/math/base/assert/is-safe-integer/benchmark/c/native/benchmark.c +++ b/lib/node_modules/@stdlib/math/base/assert/is-safe-integer/benchmark/c/native/benchmark.c @@ -92,15 +92,18 @@ static double rand_double( void ) { */ static double benchmark( void ) { double elapsed; - double x; + double x[ 100 ]; double t; bool b; int i; + for ( i = 0; i < 100; i++ ) { + x[ i ] = ( rand_double() * 1.0e7 ) - 5.0e6; + } + t = tic(); for ( i = 0; i < ITERATIONS; i++ ) { - x = ( rand_double() * 1.0e7 ) - 5.0e6; - b = stdlib_base_is_safe_integer( x ); + b = stdlib_base_is_safe_integer( x[ i%100 ] ); if ( b != true && b != false ) { printf( "should return either true or false\n" ); break; diff --git a/lib/node_modules/@stdlib/math/base/assert/is-safe-integer/test/test.js b/lib/node_modules/@stdlib/math/base/assert/is-safe-integer/test/test.js index 1df4cc23cbd8..b610d38e7721 100644 --- a/lib/node_modules/@stdlib/math/base/assert/is-safe-integer/test/test.js +++ b/lib/node_modules/@stdlib/math/base/assert/is-safe-integer/test/test.js @@ -41,7 +41,7 @@ tape( 'main export is a function', function test( t ) { tape( 'the function returns `true` if provided a safe integer', function test( t ) { var i; for ( i = -50; i < 51; i++ ) { - t.equal( isSafeInteger( i ), true, 'returns true when provided '+i ); + t.equal( isSafeInteger( i ), true, 'returns expected value when provided '+i ); } t.end(); }); @@ -51,7 +51,7 @@ tape( 'the function returns `false` if provided an integer greater than the max var i; for ( i = 1; i < 100; i++ ) { x = MAX_SAFE_INTEGER + 1 + i; - t.equal( isSafeInteger( x ), false, 'returns false when provided '+x ); + t.equal( isSafeInteger( x ), false, 'returns expected value when provided '+x ); } t.end(); }); @@ -61,7 +61,7 @@ tape( 'the function returns `false` if provided an integer less than the min saf var i; for ( i = 0; i < 100; i++ ) { x = MIN_SAFE_INTEGER - 1 - i; - t.equal( isSafeInteger( x ), false, 'returns false when provided '+x ); + t.equal( isSafeInteger( x ), false, 'returns expected value when provided '+x ); } t.end(); }); @@ -72,33 +72,33 @@ tape( 'the function returns `false` if not provided an integer', function test( for ( i = 0; i < 100; i++ ) { v = (randu()*100.0) - 50.0; if ( trunc(v) !== v ) { - t.equal( isSafeInteger( v ), false, 'returns false when provided '+v ); + t.equal( isSafeInteger( v ), false, 'returns expected value when provided '+v ); } } t.end(); }); tape( 'the function returns `true` if provided maximum safe integer', function test( t ) { - t.equal( isSafeInteger( MAX_SAFE_INTEGER ), true, 'returns true' ); + t.equal( isSafeInteger( MAX_SAFE_INTEGER ), true, 'returns expected value' ); t.end(); }); tape( 'the function returns `true` if provided minimum safe integer', function test( t ) { - t.equal( isSafeInteger( MIN_SAFE_INTEGER ), true, 'returns true' ); + t.equal( isSafeInteger( MIN_SAFE_INTEGER ), true, 'returns expected value' ); t.end(); }); tape( 'the function returns `false` if provided `NaN`', function test( t ) { - t.equal( isSafeInteger( NaN ), false, 'returns false' ); + t.equal( isSafeInteger( NaN ), false, 'returns expected value' ); t.end(); }); tape( 'the function returns `false` if provided `+infinity`', function test( t ) { - t.equal( isSafeInteger( PINF ), false, 'returns false' ); + t.equal( isSafeInteger( PINF ), false, 'returns expected value' ); t.end(); }); tape( 'the function returns `false` if provided `-infinity`', function test( t ) { - t.equal( isSafeInteger( NINF ), false, 'returns false' ); + t.equal( isSafeInteger( NINF ), false, 'returns expected value' ); t.end(); }); diff --git a/lib/node_modules/@stdlib/math/base/assert/is-safe-integer/test/test.native.js b/lib/node_modules/@stdlib/math/base/assert/is-safe-integer/test/test.native.js index 058679f15cd2..ec951c604d2b 100644 --- a/lib/node_modules/@stdlib/math/base/assert/is-safe-integer/test/test.native.js +++ b/lib/node_modules/@stdlib/math/base/assert/is-safe-integer/test/test.native.js @@ -50,7 +50,7 @@ tape( 'main export is a function', opts, function test( t ) { tape( 'the function returns `true` if provided a safe integer', opts, function test( t ) { var i; for ( i = -50; i < 51; i++ ) { - t.equal( isSafeInteger( i ), true, 'returns true when provided '+i ); + t.equal( isSafeInteger( i ), true, 'returns expected value when provided '+i ); } t.end(); }); @@ -60,7 +60,7 @@ tape( 'the function returns `false` if provided an integer greater than the max var i; for ( i = 1; i < 100; i++ ) { x = MAX_SAFE_INTEGER + 1 + i; - t.equal( isSafeInteger( x ), false, 'returns false when provided '+x ); + t.equal( isSafeInteger( x ), false, 'returns expected value when provided '+x ); } t.end(); }); @@ -70,7 +70,7 @@ tape( 'the function returns `false` if provided an integer less than the min saf var i; for ( i = 0; i < 100; i++ ) { x = MIN_SAFE_INTEGER - 1 - i; - t.equal( isSafeInteger( x ), false, 'returns false when provided '+x ); + t.equal( isSafeInteger( x ), false, 'returns expected value when provided '+x ); } t.end(); }); @@ -81,33 +81,33 @@ tape( 'the function returns `false` if not provided an integer', opts, function for ( i = 0; i < 100; i++ ) { v = (randu()*100.0) - 50.0; if ( trunc(v) !== v ) { - t.equal( isSafeInteger( v ), false, 'returns false when provided '+v ); + t.equal( isSafeInteger( v ), false, 'returns expected value when provided '+v ); } } t.end(); }); tape( 'the function returns `true` if provided maximum safe integer', opts, function test( t ) { - t.equal( isSafeInteger( MAX_SAFE_INTEGER ), true, 'returns true' ); + t.equal( isSafeInteger( MAX_SAFE_INTEGER ), true, 'returns expected value' ); t.end(); }); tape( 'the function returns `true` if provided minimum safe integer', opts, function test( t ) { - t.equal( isSafeInteger( MIN_SAFE_INTEGER ), true, 'returns true' ); + t.equal( isSafeInteger( MIN_SAFE_INTEGER ), true, 'returns expected value' ); t.end(); }); tape( 'the function returns `false` if provided `NaN`', opts, function test( t ) { - t.equal( isSafeInteger( NaN ), false, 'returns false' ); + t.equal( isSafeInteger( NaN ), false, 'returns expected value' ); t.end(); }); tape( 'the function returns `false` if provided `+infinity`', opts, function test( t ) { - t.equal( isSafeInteger( PINF ), false, 'returns false' ); + t.equal( isSafeInteger( PINF ), false, 'returns expected value' ); t.end(); }); tape( 'the function returns `false` if provided `-infinity`', opts, function test( t ) { - t.equal( isSafeInteger( NINF ), false, 'returns false' ); + t.equal( isSafeInteger( NINF ), false, 'returns expected value' ); t.end(); }); diff --git a/lib/node_modules/@stdlib/math/base/assert/uint32-is-pow2/benchmark/benchmark.js b/lib/node_modules/@stdlib/math/base/assert/uint32-is-pow2/benchmark/benchmark.js index 8829ba90c3c3..2d72357e0686 100644 --- a/lib/node_modules/@stdlib/math/base/assert/uint32-is-pow2/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/math/base/assert/uint32-is-pow2/benchmark/benchmark.js @@ -21,8 +21,7 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var randu = require( '@stdlib/random/base/randu' ); -var round = require( '@stdlib/math/base/special/round' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); var float64ToUint32 = require( '@stdlib/number/float64/base/to-uint32' ); var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; var pkg = require( './../package.json' ).name; @@ -32,14 +31,19 @@ var isPow2 = require( './../lib' ); // MAIN // bench( pkg, function benchmark( b ) { + var opts; var x; var y; var i; + opts = { + 'dtype': 'uint32' + }; + x = discreteUniform( 100, 0, 1.0e7, opts ); + b.tic(); for ( i = 0; i < b.iterations; i++ ) { - x = round( randu()*1.0e7 ); - y = isPow2( float64ToUint32( x ) ); + y = isPow2( float64ToUint32( x[ i%x.length ] ) ); if ( typeof y !== 'boolean' ) { b.fail( 'should return a boolean' ); } diff --git a/lib/node_modules/@stdlib/math/base/assert/uint32-is-pow2/benchmark/benchmark.native.js b/lib/node_modules/@stdlib/math/base/assert/uint32-is-pow2/benchmark/benchmark.native.js index c47e51aea845..803e90c3cb77 100644 --- a/lib/node_modules/@stdlib/math/base/assert/uint32-is-pow2/benchmark/benchmark.native.js +++ b/lib/node_modules/@stdlib/math/base/assert/uint32-is-pow2/benchmark/benchmark.native.js @@ -22,8 +22,7 @@ var resolve = require( 'path' ).resolve; var bench = require( '@stdlib/bench' ); -var round = require( '@stdlib/math/base/special/round' ); -var randu = require( '@stdlib/random/base/randu' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); var float64ToUint32 = require( '@stdlib/number/float64/base/to-uint32' ); var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; var tryRequire = require( '@stdlib/utils/try-require' ); @@ -41,14 +40,19 @@ var opts = { // MAIN // bench( pkg+'::native', opts, function benchmark( b ) { + var opts; var x; var y; var i; + opts = { + 'dtype': 'uint32' + }; + x = discreteUniform( 100, 0, 1.0e7, opts ); + b.tic(); for ( i = 0; i < b.iterations; i++ ) { - x = round( randu()*1.0e7 ); - y = isPow2( float64ToUint32( x ) ); + y = isPow2( float64ToUint32( x[ i%x.length ] ) ); if ( typeof y !== 'boolean' ) { b.fail( 'should return a boolean' ); } diff --git a/lib/node_modules/@stdlib/math/base/assert/uint32-is-pow2/benchmark/c/native/benchmark.c b/lib/node_modules/@stdlib/math/base/assert/uint32-is-pow2/benchmark/c/native/benchmark.c index 07ffe2d149b7..693c9cf25576 100644 --- a/lib/node_modules/@stdlib/math/base/assert/uint32-is-pow2/benchmark/c/native/benchmark.c +++ b/lib/node_modules/@stdlib/math/base/assert/uint32-is-pow2/benchmark/c/native/benchmark.c @@ -93,15 +93,18 @@ static double rand_double( void ) { */ static double benchmark( void ) { double elapsed; - double x; + double x[ 100 ]; double t; bool b; int i; + for ( i = 0; i < 100; i++ ) { + x[ i ] = ( rand_double() * 200.0 ) - 100.0; + } + t = tic(); for ( i = 0; i < ITERATIONS; i++ ) { - x = ( rand_double() * 200.0 ) - 100.0; - b = stdlib_base_uint32_is_pow2( (uint32_t)x ); + b = stdlib_base_uint32_is_pow2( (uint32_t)x[ i%100 ] ); if ( b != true && b != false ) { printf( "should return either true or false\n" ); break; diff --git a/lib/node_modules/@stdlib/math/base/assert/uint32-is-pow2/test/test.js b/lib/node_modules/@stdlib/math/base/assert/uint32-is-pow2/test/test.js index 45b1fadb1034..3e7cf21def12 100644 --- a/lib/node_modules/@stdlib/math/base/assert/uint32-is-pow2/test/test.js +++ b/lib/node_modules/@stdlib/math/base/assert/uint32-is-pow2/test/test.js @@ -35,7 +35,7 @@ tape( 'main export is a function', function test( t ) { }); tape( 'if provided `0`, the function returns false', function test( t ) { - t.strictEqual( isPow2Uint32( 0 ), false, 'returns false' ); + t.strictEqual( isPow2Uint32( 0 ), false, 'returns expected value' ); t.end(); }); diff --git a/lib/node_modules/@stdlib/math/base/assert/uint32-is-pow2/test/test.native.js b/lib/node_modules/@stdlib/math/base/assert/uint32-is-pow2/test/test.native.js index 2db112587c4b..77974b73ce3f 100644 --- a/lib/node_modules/@stdlib/math/base/assert/uint32-is-pow2/test/test.native.js +++ b/lib/node_modules/@stdlib/math/base/assert/uint32-is-pow2/test/test.native.js @@ -45,7 +45,7 @@ tape( 'main export is a function', opts, function test( t ) { tape( 'if provided `0`, the function returns false', opts, function test( t ) { var bool = isPow2Uint32( 0 ); - t.strictEqual( bool, false, 'returns false' ); + t.strictEqual( bool, false, 'returns expected value' ); t.end(); }); From e6ac586808052d81ecbf1fb75c7feab0f9eb1f00 Mon Sep 17 00:00:00 2001 From: Harsh <149176984+hrshya@users.noreply.github.com> Date: Tue, 15 Apr 2025 15:19:41 +0530 Subject: [PATCH 2/2] resolve lint issue Signed-off-by: Harsh <149176984+hrshya@users.noreply.github.com> --- .../base/assert/uint32-is-pow2/benchmark/benchmark.native.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/math/base/assert/uint32-is-pow2/benchmark/benchmark.native.js b/lib/node_modules/@stdlib/math/base/assert/uint32-is-pow2/benchmark/benchmark.native.js index 803e90c3cb77..0cf460231788 100644 --- a/lib/node_modules/@stdlib/math/base/assert/uint32-is-pow2/benchmark/benchmark.native.js +++ b/lib/node_modules/@stdlib/math/base/assert/uint32-is-pow2/benchmark/benchmark.native.js @@ -46,7 +46,7 @@ bench( pkg+'::native', opts, function benchmark( b ) { var i; opts = { - 'dtype': 'uint32' + 'dtype': 'uint32' }; x = discreteUniform( 100, 0, 1.0e7, opts );