Its been awhile since I put up a blog post and that’s partly due to being so busy working on my two Windows Phone 7 Apps. Which brings me to the topic of this post and one which I think is important for fellow devs to understand. I am one of the lucky few that have received a real device and its very nice! :) (thanks Microsoft!)
One of the first things I started to do was performance test my App on the device and found that the device is a lot slower than the emulator. So I started to run some optimizations and wrote a small benchmark App to help me work out which refactorings were the fastest. I was rather pleased with myself when I ended up getting improvements of 200 and 300%! in the emulator. Then thought I’d see how much of that new gain transferred to the device. The results shocked the hell out me, as rather than getting a gain I ended up with a lost of 50%! That’s right my optimizations did the exact opposite of what they were supposed to do.
My code processes millions of bytes and applies math during that process, some of which was using floating point math. The optimizations were designed to remove the floating point math completely in favor of integer math, because as we all know floating point math is slow right? Well it seems in the emulator that statement holds true however it didn’t seem to hold true for the actual device. To investigate this further I wrote a small app which benchmarked floating point vs integer math (simple multiplications & divisions). Here’s the results for both the emulator and the real device. Both are release builds not attached to the debugger.
| ||Multiply |
|Integer ||1,772ms ||11,174ms ||12,890ms ||79,376ms |
|Double ||6,651ms ||10,390ms ||47,898ms ||135,455ms |
|Gain of fastest ||275.34% |
|7.55 % |
based on 200,000,000 iterations
So by swapping out any double multiplications for a few integer multiplications we end up suffering badly as on the device they are roughly equal. However, the device seems to handle double divisions better and will yield a 70% gain if you use integer division math over double math. However, a great performance gainer is if you don’t need absolute precision try using the reciprocal of a number and multiply rather than divide. Ie say you want to divide a number by 255, instead multiply the number by (1/255), you can use a double variable to hold that result and then use it in your math. This alone can provide a gain of 500% on the emulator and a whopping 1,100% gain on the device!!
Be very careful about performance optimizations especially if you attempt to eliminate floating point math in favor of integer math if you don’t have a physical device. Try where you can to substitute a divide with a floating point multiply for a super-charged performance gain.