diff demo/benchmark.c @ 0:e38dacceb958

initial import
author Robert McIntyre <rlm@mit.edu>
date Fri, 21 Oct 2011 05:53:11 -0700
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/demo/benchmark.c	Fri Oct 21 05:53:11 2011 -0700
     1.3 @@ -0,0 +1,58 @@
     1.4 +/* Measures performance of SPC emulator. Takes about 4 seconds.
     1.5 +NOTE: This assumes that the program is getting all processor time; you might need to
     1.6 +arrange for this or else the performance will be reported lower than it really is.
     1.7 +
     1.8 +Usage: benchmark [test.spc]
     1.9 +*/
    1.10 +
    1.11 +#include "snes_spc/spc.h"
    1.12 +
    1.13 +#include "demo_util.h" /* error(), load_file() */
    1.14 +#include <time.h>
    1.15 +
    1.16 +clock_t start_timing( int seconds );
    1.17 +
    1.18 +int main( int argc, char** argv )
    1.19 +{
    1.20 +	/* Load SPC */
    1.21 +	long spc_size;
    1.22 +	void* spc = load_file( (argc > 1) ? argv [1] : "test.spc", &spc_size );
    1.23 +	SNES_SPC* snes_spc = spc_new();
    1.24 +	if ( !snes_spc ) error( "Out of memory" );
    1.25 +	spc_load_spc( snes_spc, spc, spc_size );
    1.26 +	free( spc );
    1.27 +	
    1.28 +	{
    1.29 +		/* Repeatedly fill buffer for 4 seconds */
    1.30 +		int const seconds = 4;
    1.31 +		#define BUF_SIZE 4096
    1.32 +		clock_t end = start_timing( seconds );
    1.33 +		int count = 0;
    1.34 +		while ( clock() < end )
    1.35 +		{
    1.36 +			static short buf [BUF_SIZE];
    1.37 +			error( spc_play( snes_spc, BUF_SIZE, buf ) );
    1.38 +			count++;
    1.39 +		}
    1.40 +		
    1.41 +		/* Report performance based on how many buffer fills were possible */
    1.42 +		{
    1.43 +			double rate = (double) count * BUF_SIZE / (spc_sample_rate * 2 * seconds);
    1.44 +			printf( "Performance: %.3fx real-time, or %.0f%% CPU for normal rate\n",
    1.45 +					rate, 100.0 / rate );
    1.46 +		}
    1.47 +	}
    1.48 +	
    1.49 +	return 0;
    1.50 +}
    1.51 +
    1.52 +/* Synchronizes with host clock and returns clock() time that is duration seconds from now */
    1.53 +clock_t start_timing( int duration )
    1.54 +{
    1.55 +	clock_t clock_dur = duration * CLOCKS_PER_SEC;
    1.56 +	clock_t time = clock();
    1.57 +	while ( clock() == time ) { }
    1.58 +	if ( clock() - time > clock_dur )
    1.59 +		error( "Insufficient clock() time resolution" );
    1.60 +	return clock() + clock_dur;
    1.61 +}