Skip to content

Commit 73d8eb8

Browse files
bench: refactor random generation in math/base/special/ceil*
PR-URL: #5834 Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent afc07e7 commit 73d8eb8

File tree

39 files changed

+204
-153
lines changed

39 files changed

+204
-153
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,22 @@ static double rand_double( void ) {
8989
* @return elapsed time in seconds
9090
*/
9191
static double benchmark( void ) {
92+
double re[ 100 ];
93+
double im[ 100 ];
9294
double elapsed;
93-
double re;
94-
double im;
9595
double t;
9696
int i;
9797

9898
double complex z;
9999

100+
for ( i = 0; i < 100; i++ ) {
101+
re[ i ] = ( 100.0*rand_double() ) - 50.0;
102+
im[ i ] = ( 100.0*rand_double() ) - 50.0;
103+
}
104+
100105
t = tic();
101106
for ( i = 0; i < ITERATIONS; i++ ) {
102-
re = ( 100.0*rand_double() ) - 50.0;
103-
im = ( 100.0*rand_double() ) - 50.0;
104-
105-
z = ( cos( re ) + I * sin( re ) ) / exp( im );
107+
z = ( cos( re[ i%100 ] ) + I * sin( re[ i%100 ] ) ) / exp( im[ i%100 ] );
106108
if ( z != z ) {
107109
printf( "should not return NaN\n" );
108110
break;

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,23 +92,26 @@ static double rand_double( void ) {
9292
* @return elapsed time in seconds
9393
*/
9494
static double benchmark( void ) {
95+
double re[ 100 ];
96+
double im[ 100 ];
9597
double elapsed;
96-
double re;
97-
double im;
9898
double t;
9999
int i;
100100

101101
stdlib_complex128_t z1;
102102
stdlib_complex128_t z2;
103103

104+
for ( i = 0; i < 100; i++ ) {
105+
re[ i ] = ( 1000.0*rand_double() ) - 500.0;
106+
im[ i ] = ( 1000.0*rand_double() ) - 500.0;
107+
}
108+
104109
t = tic();
105110
for ( i = 0; i < ITERATIONS; i++ ) {
106-
re = ( 1000.0*rand_double() ) - 500.0;
107-
im = ( 1000.0*rand_double() ) - 500.0;
108-
z1 = stdlib_complex128( re, im );
111+
z1 = stdlib_complex128( re[ i%100 ], im[ i%100 ] );
109112

110113
z2 = stdlib_base_ccis( z1 );
111-
stdlib_complex128_reim( z2, &re, &im );
114+
stdlib_complex128_reim( z2, &re[ i%100 ], &im[ i%100 ] );
112115
if ( re != re ) {
113116
printf( "should not return NaN\n" );
114117
break;

lib/node_modules/@stdlib/math/base/special/ccis/test/test.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -139,17 +139,17 @@ tape( 'if real component is `+Infinity`, all components are `NaN`', function tes
139139
var v;
140140

141141
v = ccis( new Complex128( PINF, 0.0 ) );
142-
t.strictEqual( isnan( real( v ) ), true, 'returns NaN' );
143-
t.strictEqual( isnan( imag( v ) ), true, 'returns NaN' );
142+
t.strictEqual( isnan( real( v ) ), true, 'returns expected value' );
143+
t.strictEqual( isnan( imag( v ) ), true, 'returns expected value' );
144144
t.end();
145145
});
146146

147147
tape( 'if real component is `-Infinity`, all components are `NaN`', function test( t ) {
148148
var v;
149149

150150
v = ccis( new Complex128( NINF, 0.0 ) );
151-
t.strictEqual( isnan( real( v ) ), true, 'returns NaN' );
152-
t.strictEqual( isnan( imag( v ) ), true, 'returns NaN' );
151+
t.strictEqual( isnan( real( v ) ), true, 'returns expected value' );
152+
t.strictEqual( isnan( imag( v ) ), true, 'returns expected value' );
153153
t.end();
154154
});
155155

@@ -171,24 +171,24 @@ tape( 'if imaginary component is `-Infinity`, the function computes the correct
171171
t.strictEqual( real( v ), PINF, 'returns +Infinity' );
172172

173173
// The imaginary component is computed as Infinity * 0.0 = NaN:
174-
t.strictEqual( isnan( imag( v ) ), true, 'returns NaN' );
174+
t.strictEqual( isnan( imag( v ) ), true, 'returns expected value' );
175175
t.end();
176176
});
177177

178178
tape( 'if a real or imaginary component is `NaN`, all components are `NaN`', function test( t ) {
179179
var v;
180180

181181
v = ccis( new Complex128( NaN, 3.0 ) );
182-
t.strictEqual( isnan( real( v ) ), true, 'returns NaN' );
183-
t.strictEqual( isnan( imag( v ) ), true, 'returns NaN' );
182+
t.strictEqual( isnan( real( v ) ), true, 'returns expected value' );
183+
t.strictEqual( isnan( imag( v ) ), true, 'returns expected value' );
184184

185185
v = ccis( new Complex128( 5.0, NaN ) );
186-
t.strictEqual( isnan( real( v ) ), true, 'returns NaN' );
187-
t.strictEqual( isnan( imag( v ) ), true, 'returns NaN' );
186+
t.strictEqual( isnan( real( v ) ), true, 'returns expected value' );
187+
t.strictEqual( isnan( imag( v ) ), true, 'returns expected value' );
188188

189189
v = ccis( new Complex128( NaN, NaN ) );
190-
t.strictEqual( isnan( real( v ) ), true, 'returns NaN' );
191-
t.strictEqual( isnan( imag( v ) ), true, 'returns NaN' );
190+
t.strictEqual( isnan( real( v ) ), true, 'returns expected value' );
191+
t.strictEqual( isnan( imag( v ) ), true, 'returns expected value' );
192192

193193
t.end();
194194
});

lib/node_modules/@stdlib/math/base/special/ccis/test/test.native.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -149,17 +149,17 @@ tape( 'if real component is `+Infinity`, all components are `NaN`', opts, functi
149149
var v;
150150

151151
v = ccis( new Complex128( PINF, 0.0 ) );
152-
t.strictEqual( isnan( real( v ) ), true, 'returns NaN' );
153-
t.strictEqual( isnan( imag( v ) ), true, 'returns NaN' );
152+
t.strictEqual( isnan( real( v ) ), true, 'returns expected value' );
153+
t.strictEqual( isnan( imag( v ) ), true, 'returns expected value' );
154154
t.end();
155155
});
156156

157157
tape( 'if real component is `-Infinity`, all components are `NaN`', opts, function test( t ) {
158158
var v;
159159

160160
v = ccis( new Complex128( NINF, 0.0 ) );
161-
t.strictEqual( isnan( real( v ) ), true, 'returns NaN' );
162-
t.strictEqual( isnan( imag( v ) ), true, 'returns NaN' );
161+
t.strictEqual( isnan( real( v ) ), true, 'returns expected value' );
162+
t.strictEqual( isnan( imag( v ) ), true, 'returns expected value' );
163163
t.end();
164164
});
165165

@@ -181,24 +181,24 @@ tape( 'if imaginary component is `-Infinity`, the function computes the correct
181181
t.strictEqual( real( v ), PINF, 'returns +Infinity' );
182182

183183
// The imaginary component is computed as Infinity * 0.0 = NaN:
184-
t.strictEqual( isnan( imag( v ) ), true, 'returns NaN' );
184+
t.strictEqual( isnan( imag( v ) ), true, 'returns expected value' );
185185
t.end();
186186
});
187187

188188
tape( 'if a real or imaginary component is `NaN`, all components are `NaN`', opts, function test( t ) {
189189
var v;
190190

191191
v = ccis( new Complex128( NaN, 3.0 ) );
192-
t.strictEqual( isnan( real( v ) ), true, 'returns NaN' );
193-
t.strictEqual( isnan( imag( v ) ), true, 'returns NaN' );
192+
t.strictEqual( isnan( real( v ) ), true, 'returns expected value' );
193+
t.strictEqual( isnan( imag( v ) ), true, 'returns expected value' );
194194

195195
v = ccis( new Complex128( 5.0, NaN ) );
196-
t.strictEqual( isnan( real( v ) ), true, 'returns NaN' );
197-
t.strictEqual( isnan( imag( v ) ), true, 'returns NaN' );
196+
t.strictEqual( isnan( real( v ) ), true, 'returns expected value' );
197+
t.strictEqual( isnan( imag( v ) ), true, 'returns expected value' );
198198

199199
v = ccis( new Complex128( NaN, NaN ) );
200-
t.strictEqual( isnan( real( v ) ), true, 'returns NaN' );
201-
t.strictEqual( isnan( imag( v ) ), true, 'returns NaN' );
200+
t.strictEqual( isnan( real( v ) ), true, 'returns expected value' );
201+
t.strictEqual( isnan( imag( v ) ), true, 'returns expected value' );
202202

203203
t.end();
204204
});

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
// MODULES //
2222

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

37+
x = uniform( 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 = ceil( x );
41+
y = ceil( 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 = uniform( 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.ceil( x ); // eslint-disable-line stdlib/no-builtin-math
63+
y = Math.ceil( x[ i%x.length ] ); // eslint-disable-line stdlib/no-builtin-math
6264
if ( isnan( y ) ) {
6365
b.fail( 'should not return NaN' );
6466
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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 uniform = 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 = uniform( 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 = ceil( x );
50+
y = ceil( x[ i%x.length ] );
5051
if ( isnan( y ) ) {
5152
b.fail( 'should not return NaN' );
5253
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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 = ceil( x );
104+
y = ceil( x[ i%100 ] );
102105
if ( y != y ) {
103106
printf( "should not return NaN\n" );
104107
break;

lib/node_modules/@stdlib/math/base/special/ceil/benchmark/c/cephes/benchmark.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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 = ceil( x );
109+
y = ceil( x[ i%100 ] );
107110
if ( y != y ) {
108111
printf( "should not return NaN\n" );
109112
break;

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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 = stdlib_base_ceil( x );
105+
y = stdlib_base_ceil( x[ i%100 ] );
103106
if ( y != y ) {
104107
printf( "should not return NaN\n" );
105108
break;

lib/node_modules/@stdlib/math/base/special/ceil/test/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ tape( 'the function returns the largest integer greater than or equal to a given
4646

4747
tape( 'the function returns `NaN` if provided a `NaN`', function test( t ) {
4848
var val = ceil( NaN );
49-
t.strictEqual( isnan( val ), true, 'returns NaN' );
49+
t.strictEqual( isnan( val ), true, 'returns expected value' );
5050
t.end();
5151
});
5252

lib/node_modules/@stdlib/math/base/special/ceil/test/test.native.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ tape( 'the function returns the largest integer greater than or equal to a given
5555

5656
tape( 'the function returns `NaN` if provided a `NaN`', opts, function test( t ) {
5757
var val = ceil( NaN );
58-
t.strictEqual( isnan( val ), true, 'returns NaN' );
58+
t.strictEqual( isnan( val ), true, 'returns expected value' );
5959
t.end();
6060
});
6161

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
// MODULES //
2222

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

37+
x = uniform( 100, -5.0e6, 5.0e6 );
38+
3739
b.tic();
3840
for ( i = 0; i < b.iterations; i++ ) {
39-
x = ( randu()*1.0e7 ) - 5.0e6;
40-
y = ceil10( x );
41+
y = ceil10( x[ i%x.length ] );
4142
if ( isnan( y ) ) {
4243
b.fail( 'should not return NaN' );
4344
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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 uniform = 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 = uniform( 100, -5.0e6, 5.0e6 );
47+
4648
b.tic();
4749
for ( i = 0; i < b.iterations; i++ ) {
48-
x = ( randu() * 1.0e7 ) - 5.0e6;
49-
y = ceil10( x );
50+
y = ceil10( x[ i%x.length ] );
5051
if ( isnan( y ) ) {
5152
b.fail( 'should not return NaN' );
5253
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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 ] = ( 1.0e7 * rand_double() ) - 5.0e6;
101+
}
102+
99103
t = tic();
100104
for ( i = 0; i < ITERATIONS; i++ ) {
101-
x = ( 1.0e7 * rand_double() ) - 5.0e6;
102-
y = stdlib_base_ceil10( x );
105+
y = stdlib_base_ceil10( x[ i%100 ] );
103106
if ( y != y ) {
104107
printf( "should not return NaN\n" );
105108
break;

lib/node_modules/@stdlib/math/base/special/ceil10/test/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ tape( 'the function returns `-0` if provided `-0`', function test( t ) {
5858

5959
tape( 'the function returns `NaN` if provided a `NaN`', function test( t ) {
6060
var v = ceil10( NaN );
61-
t.strictEqual( isnan( v ), true, 'returns NaN' );
61+
t.strictEqual( isnan( v ), true, 'returns expected value' );
6262
t.end();
6363
});
6464

0 commit comments

Comments
 (0)