Skip to content

Commit 8a365fc

Browse files
saurabhraghuvanshiikgrytestdlib-bot
authored
bench: refactor random number generation math/base/special/cexp*
PR-URL: #5888 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Co-authored-by: stdlib-bot <noreply@stdlib.io>
1 parent 3e1d620 commit 8a365fc

File tree

13 files changed

+105
-81
lines changed

13 files changed

+105
-81
lines changed

lib/node_modules/@stdlib/math/base/special/cexp/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 = cexp( re + I * im );
107+
z = cexp( re[ i%100 ] + I * im[ i%100 ] );
106108
if ( z != z ) {
107109
printf( "should not return NaN\n" );
108110
break;

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,20 +92,23 @@ static double rand_double( void ) {
9292
* @return elapsed time in seconds
9393
*/
9494
static double benchmark( void ) {
95+
double v[ 100 ];
9596
double elapsed;
9697
double re;
9798
double im;
9899
double t;
99-
double v;
100100
int i;
101101

102102
stdlib_complex128_t x;
103103
stdlib_complex128_t y;
104104

105+
for ( i = 0; i < 100; i++ ) {
106+
v[ i ] = ( 1000.0*rand_double() ) - 500.0;
107+
}
108+
105109
t = tic();
106110
for ( i = 0; i < ITERATIONS; i++ ) {
107-
v = ( 1000.0*rand_double() ) - 500.0;
108-
x = stdlib_complex128( v, v );
111+
x = stdlib_complex128( v[ i%100 ], v[ i%100 ] );
109112
y = stdlib_base_cexp( x );
110113
stdlib_complex128_reim( y, &re, &im );
111114
if ( re != re ) {

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -131,17 +131,17 @@ tape( 'if imaginary component is `+Infinity`, the function returns a complex num
131131
var v;
132132

133133
v = cexp( new Complex128( 0.0, PINF ) );
134-
t.strictEqual( isnan( real( v ) ), true, 'returns NaN' );
135-
t.strictEqual( isnan( imag( v ) ), true, 'returns NaN' );
134+
t.strictEqual( isnan( real( v ) ), true, 'returns expected value' );
135+
t.strictEqual( isnan( imag( v ) ), true, 'returns expected value' );
136136
t.end();
137137
});
138138

139139
tape( 'if imaginary component is `-Infinity`, the function returns a complex number having `NaN` components', function test( t ) {
140140
var v;
141141

142142
v = cexp( new Complex128( 0.0, NINF ) );
143-
t.strictEqual( isnan( real( v ) ), true, 'returns NaN' );
144-
t.strictEqual( isnan( imag( v ) ), true, 'returns NaN' );
143+
t.strictEqual( isnan( real( v ) ), true, 'returns expected value' );
144+
t.strictEqual( isnan( imag( v ) ), true, 'returns expected value' );
145145
t.end();
146146
});
147147

@@ -167,7 +167,7 @@ tape( 'if real component is `NaN` and imaginary component is `0.0`, the function
167167
var v;
168168

169169
v = cexp( new Complex128( NaN, 0.0 ) );
170-
t.strictEqual( isnan( real( v ) ), true, 'returns NaN' );
170+
t.strictEqual( isnan( real( v ) ), true, 'returns expected value' );
171171
t.strictEqual( imag( v ), 0.0, 'returns 0.0' );
172172
t.end();
173173
});
@@ -176,16 +176,16 @@ tape( 'if imaginary component is `NaN`, the function returns a complex number wi
176176
var v;
177177

178178
v = cexp( new Complex128( 5.0, NaN ) );
179-
t.strictEqual( isnan( real( v ) ), true, 'returns NaN' );
180-
t.strictEqual( isnan( imag( v ) ), true, 'returns NaN' );
179+
t.strictEqual( isnan( real( v ) ), true, 'returns expected value' );
180+
t.strictEqual( isnan( imag( v ) ), true, 'returns expected value' );
181181

182182
v = cexp( new Complex128( 0.0, NaN ) );
183-
t.strictEqual( isnan( real( v ) ), true, 'returns NaN' );
184-
t.strictEqual( isnan( imag( v ) ), true, 'returns NaN' );
183+
t.strictEqual( isnan( real( v ) ), true, 'returns expected value' );
184+
t.strictEqual( isnan( imag( v ) ), true, 'returns expected value' );
185185

186186
v = cexp( new Complex128( NaN, NaN ) );
187-
t.strictEqual( isnan( real( v ) ), true, 'returns NaN' );
188-
t.strictEqual( isnan( imag( v ) ), true, 'returns NaN' );
187+
t.strictEqual( isnan( real( v ) ), true, 'returns expected value' );
188+
t.strictEqual( isnan( imag( v ) ), true, 'returns expected value' );
189189

190190
t.end();
191191
});
@@ -210,16 +210,16 @@ tape( 'if real component is `NaN` and imaginary component is nonzero, the functi
210210
var v;
211211

212212
v = cexp( new Complex128( NaN, 3.0 ) );
213-
t.strictEqual( isnan( real( v ) ), true, 'returns NaN' );
214-
t.strictEqual( isnan( imag( v ) ), true, 'returns NaN' );
213+
t.strictEqual( isnan( real( v ) ), true, 'returns expected value' );
214+
t.strictEqual( isnan( imag( v ) ), true, 'returns expected value' );
215215
t.end();
216216
});
217217

218218
tape( 'if real component is `NaN` and imaginary component is `+0.0`, the function returns a complex number with an imaginary component equal to `+0.0`', function test( t ) {
219219
var v;
220220

221221
v = cexp( new Complex128( NaN, 0.0 ) );
222-
t.strictEqual( isnan( real( v ) ), true, 'returns NaN' );
222+
t.strictEqual( isnan( real( v ) ), true, 'returns expected value' );
223223
t.strictEqual( isPositiveZero( imag( v ) ), true, 'returns +0.0' );
224224
t.end();
225225
});
@@ -228,7 +228,7 @@ tape( 'if real component is `NaN` and imaginary component is `-0.0`, the functio
228228
var v;
229229

230230
v = cexp( new Complex128( NaN, -0.0 ) );
231-
t.strictEqual( isnan( real( v ) ), true, 'returns NaN' );
231+
t.strictEqual( isnan( real( v ) ), true, 'returns expected value' );
232232
t.strictEqual( isNegativeZero( imag( v ) ), true, 'returns +0.0' );
233233
t.end();
234234
});
@@ -238,7 +238,7 @@ tape( 'if real component is `+Infinity` and imaginary component is `+Infinity`,
238238

239239
v = cexp( new Complex128( PINF, PINF ) );
240240
t.strictEqual( real( v ), NINF, 'returns -Infinity' );
241-
t.strictEqual( isnan( imag( v ) ), true, 'returns NaN' );
241+
t.strictEqual( isnan( imag( v ) ), true, 'returns expected value' );
242242
t.end();
243243
});
244244

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -140,17 +140,17 @@ tape( 'if imaginary component is `+Infinity`, the function returns a complex num
140140
var v;
141141

142142
v = cexp( new Complex128( 0.0, PINF ) );
143-
t.strictEqual( isnan( real( v ) ), true, 'returns NaN' );
144-
t.strictEqual( isnan( imag( v ) ), true, 'returns NaN' );
143+
t.strictEqual( isnan( real( v ) ), true, 'returns expected value' );
144+
t.strictEqual( isnan( imag( v ) ), true, 'returns expected value' );
145145
t.end();
146146
});
147147

148148
tape( 'if imaginary component is `-Infinity`, the function returns a complex number having `NaN` components', opts, function test( t ) {
149149
var v;
150150

151151
v = cexp( new Complex128( 0.0, NINF ) );
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

@@ -176,7 +176,7 @@ tape( 'if real component is `NaN` and imaginary component is `0.0`, the function
176176
var v;
177177

178178
v = cexp( new Complex128( NaN, 0.0 ) );
179-
t.strictEqual( isnan( real( v ) ), true, 'returns NaN' );
179+
t.strictEqual( isnan( real( v ) ), true, 'returns expected value' );
180180
t.strictEqual( imag( v ), 0.0, 'returns 0.0' );
181181
t.end();
182182
});
@@ -185,16 +185,16 @@ tape( 'if imaginary component is `NaN`, the function returns a complex number wi
185185
var v;
186186

187187
v = cexp( new Complex128( 5.0, NaN ) );
188-
t.strictEqual( isnan( real( v ) ), true, 'returns NaN' );
189-
t.strictEqual( isnan( imag( v ) ), true, 'returns NaN' );
188+
t.strictEqual( isnan( real( v ) ), true, 'returns expected value' );
189+
t.strictEqual( isnan( imag( v ) ), true, 'returns expected value' );
190190

191191
v = cexp( new Complex128( 0.0, NaN ) );
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 = cexp( new Complex128( NaN, 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
t.end();
200200
});
@@ -219,16 +219,16 @@ tape( 'if real component is `NaN` and imaginary component is nonzero, the functi
219219
var v;
220220

221221
v = cexp( new Complex128( NaN, 3.0 ) );
222-
t.strictEqual( isnan( real( v ) ), true, 'returns NaN' );
223-
t.strictEqual( isnan( imag( v ) ), true, 'returns NaN' );
222+
t.strictEqual( isnan( real( v ) ), true, 'returns expected value' );
223+
t.strictEqual( isnan( imag( v ) ), true, 'returns expected value' );
224224
t.end();
225225
});
226226

227227
tape( 'if real component is `NaN` and imaginary component is `+0.0`, the function returns a complex number with an imaginary component equal to `+0.0`', opts, function test( t ) {
228228
var v;
229229

230230
v = cexp( new Complex128( NaN, 0.0 ) );
231-
t.strictEqual( isnan( real( v ) ), true, 'returns NaN' );
231+
t.strictEqual( isnan( real( v ) ), true, 'returns expected value' );
232232
t.strictEqual( isPositiveZero( imag( v ) ), true, 'returns +0.0' );
233233
t.end();
234234
});
@@ -237,7 +237,7 @@ tape( 'if real component is `NaN` and imaginary component is `-0.0`, the functio
237237
var v;
238238

239239
v = cexp( new Complex128( NaN, -0.0 ) );
240-
t.strictEqual( isnan( real( v ) ), true, 'returns NaN' );
240+
t.strictEqual( isnan( real( v ) ), true, 'returns expected value' );
241241
t.strictEqual( isNegativeZero( imag( v ) ), true, 'returns +0.0' );
242242
t.end();
243243
});
@@ -247,7 +247,7 @@ tape( 'if real component is `+Infinity` and imaginary component is `+Infinity`,
247247

248248
v = cexp( new Complex128( PINF, PINF ) );
249249
t.strictEqual( real( v ), NINF, 'returns -Infinity' );
250-
t.strictEqual( isnan( imag( v ) ), true, 'returns NaN' );
250+
t.strictEqual( isnan( imag( v ) ), true, 'returns expected value' );
251251
t.end();
252252
});
253253

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,21 +92,24 @@ static double rand_double( void ) {
9292
* @return elapsed time in seconds
9393
*/
9494
static double benchmark( void ) {
95+
double v[ 100 ];
9596
double elapsed;
9697
double re;
9798
double im;
9899
double t;
99-
double v;
100100
int i;
101101

102102
stdlib_complex128_t x;
103103
stdlib_complex128_t y;
104104

105+
for ( i = 0; i < 100; i++ ) {
106+
v[ i ] = ( 1000.0*rand_double() ) - 500.0;
107+
}
108+
105109
t = tic();
106110
for ( i = 0; i < ITERATIONS; i++ ) {
107-
v = ( 1000.0*rand_double() ) - 500.0;
108-
x = stdlib_complex128( v, v );
109-
y = stdlib_base_cflipsign( x, -v );
111+
x = stdlib_complex128( v[ i%100 ], v[ i%100 ] );
112+
y = stdlib_base_cflipsign( x, -v[ i%100 ] );
110113
stdlib_complex128_reim( y, &re, &im );
111114
if ( re != re ) {
112115
printf( "unexpected result\n" );

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,18 +91,21 @@ static float rand_float( void ) {
9191
* @return elapsed time in seconds
9292
*/
9393
static double benchmark( void ) {
94+
float v[ 100 ];
9495
float complex x;
9596
float complex y;
9697
double elapsed;
9798
double t;
98-
float v;
9999
int i;
100100

101+
for ( i = 0; i < 100; i++ ) {
102+
v[ i ] = ( 1000.0f*rand_float() ) - 500.0f;
103+
}
104+
101105
t = tic();
102106
for ( i = 0; i < ITERATIONS; i++ ) {
103-
v = ( 1000.0f*rand_float() ) - 500.0f;
104-
x = v + v*I;
105-
y = stdlib_base_cflipsignf( x, -v );
107+
x = v[ i%100 ] + v[ i%100 ]*I;
108+
y = stdlib_base_cflipsignf( x, -v[ i%100 ] );
106109
if ( crealf( y ) != crealf( y ) ) {
107110
printf( "unexpected result\n" );
108111
break;

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

Lines changed: 5 additions & 4 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 randu = require( '@stdlib/random/array/uniform' );
2525
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2626
var Complex128 = require( '@stdlib/complex/float64/ctor' );
2727
var real = require( '@stdlib/complex/float64/real' );
@@ -66,11 +66,12 @@ bench( pkg+'::manual', function benchmark( b ) {
6666
var y;
6767
var i;
6868

69+
re = randu( 10, -500.0, 500.0 );
70+
im = randu( 10, -500.0, 500.0 );
71+
6972
b.tic();
7073
for ( i = 0; i < b.iterations; i++ ) {
71-
re = ( randu()*1000.0 ) - 500.0;
72-
im = ( randu()*1000.0 ) - 500.0;
73-
y = [ floor( re ), floor( im ) ];
74+
y = [ floor( re[ i%re.length ] ), floor( im[ i%im.length ] ) ];
7475
if ( y.length === 0 ) {
7576
b.fail( 'should not be empty' );
7677
}

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,23 @@ 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
double complex y;
100100

101+
for ( i = 0; i < 100; i++ ) {
102+
re[ i ] = ( 1000.0*rand_double() ) - 500.0;
103+
im[ i ] = ( 1000.0*rand_double() ) - 500.0;
104+
}
105+
101106
t = tic();
102107
for ( i = 0; i < ITERATIONS; i++ ) {
103-
re = ( 1000.0*rand_double() ) - 500.0;
104-
im = ( 1000.0*rand_double() ) - 500.0;
105-
z = re + im*I;
108+
z = re[ i%100 ] + im[ i%100 ]*I;
106109
y = floor( creal(z) ) + floor( cimag(z) )*I;
107110
if ( y != y ) {
108111
printf( "should not return NaN\n" );

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,20 +92,23 @@ static double rand_double( void ) {
9292
* @return elapsed time in seconds
9393
*/
9494
static double benchmark( void ) {
95+
double v[ 100 ];
9596
double elapsed;
9697
double re;
9798
double im;
9899
double t;
99-
double v;
100100
int i;
101101

102102
stdlib_complex128_t x;
103103
stdlib_complex128_t y;
104104

105+
for ( i = 0; i < 100; i++ ) {
106+
v[ i ] = ( 1000.0*rand_double() ) - 500.0;
107+
}
108+
105109
t = tic();
106110
for ( i = 0; i < ITERATIONS; i++ ) {
107-
v = ( 1000.0*rand_double() ) - 500.0;
108-
x = stdlib_complex128( v, v );
111+
x = stdlib_complex128( v[ i%100 ], v[ i%100 ] );
109112
y = stdlib_base_cfloorn( x, -2 );
110113
stdlib_complex128_reim( y, &re, &im );
111114
if ( re != re ) {

0 commit comments

Comments
 (0)