annotate demo/benchmark.c @ 0:e38dacceb958

initial import
author Robert McIntyre <rlm@mit.edu>
date Fri, 21 Oct 2011 05:53:11 -0700
parents
children
rev   line source
rlm@0 1 /* Measures performance of SPC emulator. Takes about 4 seconds.
rlm@0 2 NOTE: This assumes that the program is getting all processor time; you might need to
rlm@0 3 arrange for this or else the performance will be reported lower than it really is.
rlm@0 4
rlm@0 5 Usage: benchmark [test.spc]
rlm@0 6 */
rlm@0 7
rlm@0 8 #include "snes_spc/spc.h"
rlm@0 9
rlm@0 10 #include "demo_util.h" /* error(), load_file() */
rlm@0 11 #include <time.h>
rlm@0 12
rlm@0 13 clock_t start_timing( int seconds );
rlm@0 14
rlm@0 15 int main( int argc, char** argv )
rlm@0 16 {
rlm@0 17 /* Load SPC */
rlm@0 18 long spc_size;
rlm@0 19 void* spc = load_file( (argc > 1) ? argv [1] : "test.spc", &spc_size );
rlm@0 20 SNES_SPC* snes_spc = spc_new();
rlm@0 21 if ( !snes_spc ) error( "Out of memory" );
rlm@0 22 spc_load_spc( snes_spc, spc, spc_size );
rlm@0 23 free( spc );
rlm@0 24
rlm@0 25 {
rlm@0 26 /* Repeatedly fill buffer for 4 seconds */
rlm@0 27 int const seconds = 4;
rlm@0 28 #define BUF_SIZE 4096
rlm@0 29 clock_t end = start_timing( seconds );
rlm@0 30 int count = 0;
rlm@0 31 while ( clock() < end )
rlm@0 32 {
rlm@0 33 static short buf [BUF_SIZE];
rlm@0 34 error( spc_play( snes_spc, BUF_SIZE, buf ) );
rlm@0 35 count++;
rlm@0 36 }
rlm@0 37
rlm@0 38 /* Report performance based on how many buffer fills were possible */
rlm@0 39 {
rlm@0 40 double rate = (double) count * BUF_SIZE / (spc_sample_rate * 2 * seconds);
rlm@0 41 printf( "Performance: %.3fx real-time, or %.0f%% CPU for normal rate\n",
rlm@0 42 rate, 100.0 / rate );
rlm@0 43 }
rlm@0 44 }
rlm@0 45
rlm@0 46 return 0;
rlm@0 47 }
rlm@0 48
rlm@0 49 /* Synchronizes with host clock and returns clock() time that is duration seconds from now */
rlm@0 50 clock_t start_timing( int duration )
rlm@0 51 {
rlm@0 52 clock_t clock_dur = duration * CLOCKS_PER_SEC;
rlm@0 53 clock_t time = clock();
rlm@0 54 while ( clock() == time ) { }
rlm@0 55 if ( clock() - time > clock_dur )
rlm@0 56 error( "Insufficient clock() time resolution" );
rlm@0 57 return clock() + clock_dur;
rlm@0 58 }