
Got a question about Java? Java discussion forum
The cost of Java object orientation in mathematical calculations: an example with Newton's method fractalsIn various program designs, an important question arises: to what extent should data be encapsulated into separate objects, and what affect this encapsulation will have on performance? The question becomes particularly crucial when the most obvious encapsulation from a program design and maintenance point of view would lead to a large number of objects being created and to rapid object throughput (the creation of a relatively large number of objects in a given space of time). In this article, I examine the case of calculation with complex numbers. If you are not familiar with the concept of complex numbers, essentially they involve a type of calculation where:
So the question we essentially address is: how much worse in terms of performance is the maintainable, objectoriented solution compared to the "handcranked" optimisation that avoids object creation? Our test task will consist of generating a fractal image based on Newton's method, as in Figure 1 below: This is essentially a computationally expensive task that relies on complex numbers and, in the case of the formula that we choose to generate the image, it is relatively straightforward to create an optimised version that avoids object encapsulation. We find that:
On the next pages, we look at the background to the problem, with an overview of complex numbers and Newton's method. If you're familiar with these two concepts, you may want to skip forward to the section on fractals with Newton's method, or even skip straight forward to the section discussing optimisation and performance measurements of the object oriented approach to complex number calculation. Copyright © Javamex UK 2010. All rights reserved. 