Skip to content
Merged
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 @@ -19,27 +19,40 @@
#include "stdlib/stats/base/dists/anglit/quantile.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <sys/time.h>

#define NAME "anglit-quantile"
#define ITERATIONS 1000000
#define REPEATS 3

/**
* Prints the TAP version.
*/
static void print_version( void ) {
printf( "TAP version 13\n" );
}

/**
* Prints the TAP summary.
*
* @param total total number of tests
* @param passing total number of passing tests
*/
static void print_summary( int total, int passing ) {
printf( "#\n" );
printf( "1..%d\n", total );
printf( "1..%d\n", total ); // TAP plan
printf( "# total %d\n", total );
printf( "# pass %d\n", passing );
printf( "#\n" );
printf( "# ok\n" );
}

/**
* Prints benchmarks results.
*
* @param elapsed elapsed time in seconds
*/
static void print_results( double elapsed ) {
double rate = (double)ITERATIONS / elapsed;
printf( " ---\n" );
Expand All @@ -49,17 +62,34 @@ static void print_results( double elapsed ) {
printf( " ...\n" );
}

/**
* Returns a clock time.
*
* @return clock time
*/
static double tic( void ) {
struct timeval now;
gettimeofday( &now, NULL );
return (double)now.tv_sec + (double)now.tv_usec/1.0e6;
}

/**
* Generates a random number on the interval [min,max).
*
* @param min minimum value (inclusive)
* @param max maximum value (exclusive)
* @return random number
*/
static double random_uniform( const double min, const double max ) {
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
return min + ( v*(max-min) );
}

/**
* Runs a benchmark.
*
* @return elapsed time in seconds
*/
static double benchmark( void ) {
double sigma[ 100 ];
double mu[ 100 ];
Expand All @@ -78,31 +108,35 @@ static double benchmark( void ) {
t = tic();
for ( i = 0; i < ITERATIONS; i++ ) {
y = stdlib_base_dists_anglit_quantile( p[ i % 100 ], mu[ i % 100 ], sigma[ i % 100 ] );
if ( isnan( y ) ) {
if ( y != y ) {
printf( "should not return NaN\n" );
break;
}
}
elapsed = tic() - t;

if ( isnan( y ) ) {
if ( y != y ) {
printf( "should not return NaN\n" );
}
return elapsed;
}

/**
* Main execution sequence.
*/
int main( void ) {
double elapsed;
int i;

// Use the current time to seed the random number generator:
srand( time( NULL ) );

print_version();
for ( i = 0; i < REPEATS; i++ ) {
printf( "# c::%s\n", NAME );
elapsed = benchmark();
print_results( elapsed );
printf( "ok %d benchmark finished\n", i + 1 );
printf( "ok %d benchmark finished\n", i+1 );
}
print_summary( REPEATS, REPEATS );
}