# Techniques for benchmarking code running on the Sega Genesis' 68000 CPU?

I'm playing around with writing code for the Sega Genesis, which uses a Motorola 68000. I'm not an assembly code guru by any means so I'm writing in C and using SGDK. As it's slower than assembly, I'd like to be able to benchmark so that I can find slow spots and optimize them as much as I can. But I'm at a loss of how to do that. It doesn't look like the CPU has any timers or ticks that I can use. Any ideas?

One idea I've got is to find the code I want to benchmark and set it to run 1000 or 10,000 times, and have something visible happen when it's done. Because it's looping many, many times, the amount of time it takes is measurable with a stopwatch. Not the most friendly way, but it might be possible to use in some cases.

## 1 Answer

Look at the timer.h include, it has some of the basic timing functions. For reference http://code.google.com/p/sgdk/source/browse/trunk/include/timer.h?r=40

For example this function:

// return elapsed ticks from console reset (1/300 second based)
u32  getTick();


So, if you wanted to test a piece of code you would do this:

u32 start = getTick();
// place code here
u32 dif = getTick() - start;
// 'dif' would be the number ticks that passed.

• D'oh! I should've looked at the SGDK headers! I'll try that out and see if it works for me. – DJCouchyCouch Mar 13 '12 at 15:08
• @ShawnLeBlanc remember to run each piece of code a good number of times: excess of 1000. This will hopefully iron out any random conditions that may occur while the code is executing; and will avoid limitations regarding the timer frequency. (I'm sure you know this though - kudos for profiling first). – Jonathan Dickinson Mar 13 '12 at 15:24
• @ShawnLeBlanc You can use a this clever trick and just #DEFINE something for your profiling builds (leave it empty for DEBUG/RELEASE). – Jonathan Dickinson Mar 13 '12 at 15:33