Skip to content

Commit 201d444

Browse files
bench: refactor to use dynamic memory allocation in math/strided/special/strunc
1 parent 6418750 commit 201d444

1 file changed

Lines changed: 89 additions & 71 deletions

File tree

lib/node_modules/@stdlib/math/strided/special/strunc/benchmark/c/benchmark.length.c

Lines changed: 89 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* you may not use this file except in compliance with the License.
88
* You may obtain a copy of the License at
99
*
10-
* http://www.apache.org/licenses/LICENSE-2.0
10+
* http://www.apache.org/licenses/LICENSE-2.0
1111
*
1212
* Unless required by applicable law or agreed to in writing, software
1313
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -33,7 +33,7 @@
3333
* Prints the TAP version.
3434
*/
3535
static void print_version( void ) {
36-
printf( "TAP version 13\n" );
36+
printf( "TAP version 13\n" );
3737
}
3838

3939
/**
@@ -43,12 +43,12 @@ static void print_version( void ) {
4343
* @param passing total number of passing tests
4444
*/
4545
static void print_summary( int total, int passing ) {
46-
printf( "#\n" );
47-
printf( "1..%d\n", total ); // TAP plan
48-
printf( "# total %d\n", total );
49-
printf( "# pass %d\n", passing );
50-
printf( "#\n" );
51-
printf( "# ok\n" );
46+
printf( "#\n" );
47+
printf( "1..%d\n", total ); // TAP plan
48+
printf( "# total %d\n", total );
49+
printf( "# pass %d\n", passing );
50+
printf( "#\n" );
51+
printf( "# ok\n" );
5252
}
5353

5454
/**
@@ -58,12 +58,12 @@ static void print_summary( int total, int passing ) {
5858
* @param elapsed elapsed time in seconds
5959
*/
6060
static void print_results( int iterations, double elapsed ) {
61-
double rate = (double)iterations / elapsed;
62-
printf( " ---\n" );
63-
printf( " iterations: %d\n", iterations );
64-
printf( " elapsed: %0.9f\n", elapsed );
65-
printf( " rate: %0.9f\n", rate );
66-
printf( " ...\n" );
61+
double rate = (double)iterations / elapsed;
62+
printf( " ---\n" );
63+
printf( " iterations: %d\n", iterations );
64+
printf( " elapsed: %0.9f\n", elapsed );
65+
printf( " rate: %0.9f\n", rate );
66+
printf( " ...\n" );
6767
}
6868

6969
/**
@@ -72,9 +72,9 @@ static void print_results( int iterations, double elapsed ) {
7272
* @return clock time
7373
*/
7474
static double tic( void ) {
75-
struct timeval now;
76-
gettimeofday( &now, NULL );
77-
return (double)now.tv_sec + (double)now.tv_usec/1.0e6;
75+
struct timeval now;
76+
gettimeofday( &now, NULL );
77+
return (double)now.tv_sec + (double)now.tv_usec/1.0e6;
7878
}
7979

8080
/**
@@ -85,13 +85,13 @@ static double tic( void ) {
8585
* @return random number
8686
*/
8787
double rand_uniform( double a, double b ) {
88-
double x;
89-
int r;
88+
double x;
89+
int r;
9090

91-
r = rand();
92-
x = (double)r / ( (double)RAND_MAX + 1.0 );
91+
r = rand();
92+
x = (double)r / ( (double)RAND_MAX + 1.0 );
9393

94-
return ( b*x ) + ( (1.0-x)*a );
94+
return ( b*x ) + ( (1.0-x)*a );
9595
}
9696

9797
/**
@@ -102,7 +102,7 @@ double rand_uniform( double a, double b ) {
102102
* @return random number
103103
*/
104104
float rand_uniformf( float a, float b ) {
105-
return (float)rand_uniform( (double)a, (double)b );
105+
return (float)rand_uniform( (double)a, (double)b );
106106
}
107107

108108
/**
@@ -113,57 +113,75 @@ float rand_uniformf( float a, float b ) {
113113
* @return elapsed time in seconds
114114
*/
115115
static double benchmark( int iterations, int len ) {
116-
double elapsed;
117-
float x[ len ];
118-
float y[ len ];
119-
double t;
120-
int i;
121-
122-
for ( i = 0; i < len; i++ ) {
123-
x[ i ] = rand_uniformf( -10.0f, 10.0f );
124-
y[ i ] = 0.0f;
125-
}
126-
t = tic();
127-
for ( i = 0; i < iterations; i++ ) {
128-
stdlib_strided_strunc( len, x, 1, y, 1 );
129-
if ( y[ 0 ] != y[ 0 ] ) {
130-
printf( "should not return NaN\n" );
131-
break;
132-
}
133-
}
134-
elapsed = tic() - t;
135-
if ( y[ 0 ] != y[ 0 ] ) {
136-
printf( "should not return NaN\n" );
137-
}
138-
return elapsed;
116+
double elapsed;
117+
float *x;
118+
float *y;
119+
double t;
120+
int i;
121+
122+
x = (float *)malloc( len * sizeof( float ) );
123+
y = (float *)malloc( len * sizeof( float ) );
124+
125+
if ( x == NULL || y == NULL ) {
126+
if ( x != NULL ) {
127+
free( x );
128+
}
129+
if ( y != NULL ) {
130+
free( y );
131+
}
132+
printf( "Error: unable to allocate memory.\n" );
133+
exit( 1 );
134+
}
135+
136+
for ( i = 0; i < len; i++ ) {
137+
x[ i ] = rand_uniformf( -10.0f, 10.0f );
138+
y[ i ] = 0.0f;
139+
}
140+
t = tic();
141+
for ( i = 0; i < iterations; i++ ) {
142+
stdlib_strided_strunc( len, x, 1, y, 1 );
143+
if ( y[ 0 ] != y[ 0 ] ) {
144+
printf( "should not return NaN\n" );
145+
break;
146+
}
147+
}
148+
elapsed = tic() - t;
149+
if ( y[ 0 ] != y[ 0 ] ) {
150+
printf( "should not return NaN\n" );
151+
}
152+
153+
free( x );
154+
free( y );
155+
156+
return elapsed;
139157
}
140158

141159
/**
142160
* Main execution sequence.
143161
*/
144162
int main( void ) {
145-
double elapsed;
146-
int count;
147-
int iter;
148-
int len;
149-
int i;
150-
int j;
151-
152-
// Use the current time to seed the random number generator:
153-
srand( time( NULL ) );
154-
155-
print_version();
156-
count = 0;
157-
for ( i = MIN; i <= MAX; i++ ) {
158-
len = pow( 10, i );
159-
iter = ITERATIONS / pow( 10, i-1 );
160-
for ( j = 0; j < REPEATS; j++ ) {
161-
count += 1;
162-
printf( "# c::%s:len=%d\n", NAME, len );
163-
elapsed = benchmark( iter, len );
164-
print_results( iter, elapsed );
165-
printf( "ok %d benchmark finished\n", count );
166-
}
167-
}
168-
print_summary( count, count );
169-
}
163+
double elapsed;
164+
int count;
165+
int iter;
166+
int len;
167+
int i;
168+
int j;
169+
170+
// Use the current time to seed the random number generator:
171+
srand( time( NULL ) );
172+
173+
print_version();
174+
count = 0;
175+
for ( i = MIN; i <= MAX; i++ ) {
176+
len = pow( 10, i );
177+
iter = ITERATIONS / pow( 10, i-1 );
178+
for ( j = 0; j < REPEATS; j++ ) {
179+
count += 1;
180+
printf( "# c::%s:len=%d\n", NAME, len );
181+
elapsed = benchmark( iter, len );
182+
print_results( iter, elapsed );
183+
printf( "ok %d benchmark finished\n", count );
184+
}
185+
}
186+
print_summary( count, count );
187+
}

0 commit comments

Comments
 (0)