Skip to content

bench: replace stack allocation with dynamic allocation #7317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,24 @@ float rand_uniformf( float a, float b ) {
*/
static double benchmark( int iterations, int len ) {
double elapsed;
double x[ len ];
double y[ len ];
double *x;
double *y;
double t;
int i;

// Allocate memory dynamically to avoid stack overflow
x = malloc( len * sizeof(double) );
if ( x == NULL ) {
printf( "Error: failed to allocate memory for x array\n" );
return -1.0;
}
y = malloc( len * sizeof(double) );
if ( y == NULL ) {
printf( "Error: failed to allocate memory for y array\n" );
free( x );
return -1.0;
}

for ( i = 0; i < len; i++ ) {
x[ i ] = rand_uniform( -10.0, 10.0 );
y[ i ] = 0.0;
Expand All @@ -136,6 +149,9 @@ static double benchmark( int iterations, int len ) {
if ( y[ 0 ] != y[ 0 ] ) {
printf( "should not return NaN\n" );
}
// Free allocated memory
free( x );
free( y );
return elapsed;
}

Expand All @@ -162,6 +178,10 @@ int main( void ) {
count += 1;
printf( "# c::%s:len=%d\n", NAME, len );
elapsed = benchmark( iter, len );
if ( elapsed < 0.0 ) {
printf( "not ok %d benchmark failed - memory allocation error\n", count );
continue;
}
print_results( iter, elapsed );
printf( "ok %d benchmark finished\n", count );
}
Expand Down
Loading