The Need for Speed: coding styles for Mobile Devices

3 downloads 0 Views 1MB Size Report
Jan 31, 2007 - for floating point calculations to be carried out using fixed point integer arithmetic. A selection of these libraries include MathFP [4], MicroFloat /.
The Need for Speed: coding styles for Mobile Devices Daniel C. Doolan, Sabin Tabirca Department of Computer Science, University College Cork, College Road, Cork, Ireland. E-mail: [email protected], [email protected]

Abstract – The very mention of the words “mobile application” one almost always thinks of “Java”. Just about every mobile phone that is currently produced has a Java Virtual Machine as standard. Object Orientation is one of the key features of Java language. Is Object Orientation under the mobile environment more of a hinderance than a help? How does an Object Orientated solution compare to a more structured approach. How does the constant need for call methods compare with the inlining of the method functions themselves. How much of an advantage is Fixed Point Integer notation over double or floating point values in terms of computation speed and accuracy. These are some of the questions we wish to answer, by giving practical results from the domain of fractal generation. Keywords – Object Orientation, Inlining, Mobile Phone, PDA, Fractals.

1. INTRODUCTION Not too long ago circa 1980's the art of game programming was the domain of the skilled few because complex solutions were necessary to get the best performance out of a computer with (at the time) very limited resources. The advancement in both computing power and resources has advanced considerably over the intervening years. This resulted in the programmer not having to develop the most efficient code possible, as there is now more than enough computing power to render games with a high frame rate. The recent proliferation of mobile devices onto the market in just a few years has once again called for the need to develop code that is capable of running quickly while using very limited resources. All modern phones have a JVM built in as standard. Due to the ease with which a Java application may be installed on to such a device the market for Java based applications has exploded. More and more are we seeing mobile gaming becoming all the more popular, as it is gaining a larger market share of the gaming industry each year. Only recently have we seen the shift from uninteresting 2D platform type games to far more interactive 3D First Person Shooter and Driving type games. This has been made possible with libraries such as JSR184 (Java 3D for mobile devices) [9] and OpenGL-ES [5][11][14]. When one thinks of Java the first keywords that come to mind is Object Orientation (OO). The problem with OO on the mobile device is that applications with significant method calls will actually work far slower than a program with a more structured style. The reason the OO application runs slower is because it takes time to call a method and return from same. The golden rule for efficient mobile development is to develop using a non OO approach. More often than not a program that has

very long and unwieldy program source code will run far better than a very neatly designed OO styled application. 1.1. Motivation Their are several third party libraries available for carrying out Fixed Point Integer calculations. Many of the earlier mobile devices did not support CLDC1.1 (which has support for the Float and Double data types), hence an alternative means of representing Real numbers proved necessary. The added bonus of Fixed Point Integer calculations is that they typically execute faster than floating point operations. Therefore to develop optimal code one should investigate these resources to see which provides the best overall computation. The generation of Fractal images inherently requires significant mathematical processing, thus proving to be a suitable test bed to analyse the optimal coding technique to reduce the processing time.

2. FRACTAL GENERATION Fractal Generation was the application of choice for the testing of the processing times of the various techniques and libraries, because the process of generating fractals is a processor intensive task. The Mandelbrot Set (MSet) in particular was chosen as the application to test the processing capabilities of the various techniques. The generation of the MSet is an iterative one, whereby for every point on the image a new set of Complex Numbers are generated based on that point. An inner loop executes up to a predefined number of iterations. At each iteration of the loop a generating function is executed for example Z2+C. Should the absolute value of this new result lie beyond a threshold R then the pixel at the current

location will be updated with a particular colour (Listings 1,2). Listings 1 and 2 show the structure of the MSet generation function. Listing 2 shows an OO approach, at each iteration through the loop a new Complex Object is created. The Complex class itself has inbuilt static methods to carryout the essential operations such as Complex Product and Complex summation. The OO design allows for very elegant and user friendly code to be developed. Comparing this to Listing 1, one immediately sees more complexity in the generating function. In lining calculations as such can significantly improve the processing times, but reduces readability for the programmer. public void generateFractal(){ processingTime = System.currentTimeMillis(); for(i=0;i