rlm@0: /* Measures performance of SPC emulator. Takes about 4 seconds. rlm@0: NOTE: This assumes that the program is getting all processor time; you might need to rlm@0: arrange for this or else the performance will be reported lower than it really is. rlm@0: rlm@0: Usage: benchmark [test.spc] rlm@0: */ rlm@0: rlm@0: #include "snes_spc/spc.h" rlm@0: rlm@0: #include "demo_util.h" /* error(), load_file() */ rlm@0: #include rlm@0: rlm@0: clock_t start_timing( int seconds ); rlm@0: rlm@0: int main( int argc, char** argv ) rlm@0: { rlm@0: /* Load SPC */ rlm@0: long spc_size; rlm@0: void* spc = load_file( (argc > 1) ? argv [1] : "test.spc", &spc_size ); rlm@0: SNES_SPC* snes_spc = spc_new(); rlm@0: if ( !snes_spc ) error( "Out of memory" ); rlm@0: spc_load_spc( snes_spc, spc, spc_size ); rlm@0: free( spc ); rlm@0: rlm@0: { rlm@0: /* Repeatedly fill buffer for 4 seconds */ rlm@0: int const seconds = 4; rlm@0: #define BUF_SIZE 4096 rlm@0: clock_t end = start_timing( seconds ); rlm@0: int count = 0; rlm@0: while ( clock() < end ) rlm@0: { rlm@0: static short buf [BUF_SIZE]; rlm@0: error( spc_play( snes_spc, BUF_SIZE, buf ) ); rlm@0: count++; rlm@0: } rlm@0: rlm@0: /* Report performance based on how many buffer fills were possible */ rlm@0: { rlm@0: double rate = (double) count * BUF_SIZE / (spc_sample_rate * 2 * seconds); rlm@0: printf( "Performance: %.3fx real-time, or %.0f%% CPU for normal rate\n", rlm@0: rate, 100.0 / rate ); rlm@0: } rlm@0: } rlm@0: rlm@0: return 0; rlm@0: } rlm@0: rlm@0: /* Synchronizes with host clock and returns clock() time that is duration seconds from now */ rlm@0: clock_t start_timing( int duration ) rlm@0: { rlm@0: clock_t clock_dur = duration * CLOCKS_PER_SEC; rlm@0: clock_t time = clock(); rlm@0: while ( clock() == time ) { } rlm@0: if ( clock() - time > clock_dur ) rlm@0: error( "Insufficient clock() time resolution" ); rlm@0: return clock() + clock_dur; rlm@0: }