Chapter 8: Faster Math

image from book Download CD Content

Output looks bizarre.Oops that was supposed to beAdd not multiply.—Anonymous Game Developer

Introduction—Java Math Performance

Improving mathematical performance in Java has been a topic of interest since Java’s creation. When Java was available only as interpreted VMs, many tips and tricks were developed to squeeze the maximum performance out of any Java code because the interpreted mode was so slow compared to native code. In addition, PCs and other computer architectures have special CPU instructions specifically designed for accelerating certain math operations, and Java programmers could not take advantage of them.

At that time, serious math processing was barely on the radar for Java developers. Eventually, Java’s JIT compiler was developed and provided such a huge leap in performance that the numeric community took a deeper look at Java. It wasn’t long before scientists and developers working on complex and process-intensive mathematics began developing in Java to get all the benefits that other Java developers had enjoyed. Now the latest VMs even take advantage of the specialized instructions previously available only to native-language programmers.

JIT compiling is a huge performance improvement over the interpreted mode, but many math operations are still behind the speed of native languages. This lag happens because of several reasons because part of the Java language design and ever-improved JIT compilers cannot completely remedy it. Also, alternate approximation math methods can be used that can greatly improve performance but that are not necessarily obvious to implement. Their implementation even counters good object-oriented design as well. To make matters worse, what seems to be a global solution may not be globally optimal; what may be a good solution in one small loop may not scale at all, or can be a maintenance nightmare when used throughout. Finding the right balance between usability and performance is often tedious and error prone, but luckily, in the game-programming domain we have the luxury of trading accuracy for speed in many cases. With carefully made trades, we can create great solutions for many core game-math problems.

In this chapter, we will attempt to address several heavily used game-math operations as well as offer a guide in design for future math code. The chapter contains three main sections. The first looks at general math-function usage improvements. The second examines complex functions, such as sine, and functions that typically have iterative solutions, such as square root. This requires that the calling code be a single method or function (x = sqrt(y), for example), but the solution requires the CPU to run many cycles in finding the solution. This class of math operations is a problem in any language, not just Java, and has well-explored solutions. We will implement and test these in Java to see what gains can be made.

The third section focuses on vector and matrix operations. These operations typically use many repetitive operations in high-level code (for example, add and multiply in a matrix multiple) and are required for 3D graphics and games. This code can be tuned specifically for Java, that is, to be as optimized as possible for the Java VM. Because the Java VM is somewhat of a moving target and may have different execution characteristics on different platforms, guaranteed optimal solutions are unlikely, but, in general, well-written solutions can be nearly optimal across a large class of platforms.



Practical Java Game Programming
Practical Java Game Programming (Charles River Media Game Development)
ISBN: 1584503262
EAN: 2147483647
Year: 2003
Pages: 171

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net