When writing realtime applications optimal code is always the goal. But it’s hard to always know exactly what is going to be “expensive” in terms of CPU time.

Java Performance Tuning is a very comprehensive optimization resource, but a bit too dense for less experienced programmers. JavaWorld has an article called Make Java fast: Optimize!, which is more accessibly and explains some basics quite well. More importantly, it provides a benchmark applet that gives an overview of the relative CPU cost of basic Java instructions.

For instance, the assignment “int x = 0″ would take 987 picoseconds to execute a certain number of times, while “x[0]=x[10]” would take 1304 picoseconds. See below for benchmark examples.

There is also the classic Jonathan Hardwick optimization tutorial, which is a bit more technical. It’s old (1998), but the basic concepts are still valid. One thing he warns against is the cost of object instantiation. Creating a new object instance can be expensive, particularly if it’s a complex object with lots of data. Try recycling complex objects rather than creating new ones.

Keep in mind that spending tons of time optimizing single lines of code is usually not worth it. Small differences in the benchmarks below would only matter if you’re doing some calculation a huge amount of times. Try using a profiler if you have the time and the need to figure out which parts of your program suck CPU cycles.

Otherwise, some basic rules of thumb should get you a long way:

  • Don’t calculate values more than once. Instead, store them in a local variable for reuse.
  • Avoid creating new complex objects when you could recycle old instances instead.
  • If possible, use simple data structures. A one-dimensional array is faster to access than a two-dimensional one.
  • If you’re doing work with Strings, consider using StringBuffer to avoid instantiating temporary String objects.
Benchmarks

Here is some output from the JavaWorld Benchmark Applet. First is a mixed benchmark giving a range of basic stats (”ps” is picoseconds):

=== .Mixed Benchmark ===
for ( ; i < local; i++) 588 ps
for ( ; --i >= 0; ) 587 ps
local int x = x -1 ps
local int array[0] = array[0] 1304 ps
static int x = x 987 ps
instance int x = x 876 ps
static method() {return 0;} -3 ps
instance int method() {return 0;} -3 ps
instance synchronized method() {return 0;} 42192 ps
local byte += byte 575 ps
local short += short 172 ps
local int += int -3 ps
local long += long 435 ps
local float += float 434 ps
local double += double 434 ps
Subclass s = (Subclass) superclass 2189 ps
Interface i = (Interface) object 1743 ps
new Object() 4962 ps
Throw and catch new Exception() 896556 ps
=== .Mixed TOTAL: 954024 ps
*** done: 1:02 (tests 0:55) ***

Here are benchmarks for math operators, omitting data types like byte, short and long that aren’t commonly used by Processing coders. I’m including doubles, to show the speed difference between doubles and floats.

=== .Operator Benchmark ===
--- int operators, local vars
int++ -1 ps
int += int -1 ps
int = int + int -2 ps
int *= int 436 ps
int = int * int 96 ps
int *= 2 -2 ps
int <<= 1 -1 ps
int %= int 8083 ps
int = int % int 4852 ps
int /= int 8092 ps
int = int / int 4469 ps
int /= 2 8044 ps
int >>= 1 -1 ps
int >>= int 338 ps
int = int >> int 438 ps
int |= int 0 ps
int = int | int -2 ps
int &= int -2 ps
int = int & int -2 ps
int ^= int 0 ps
int = int ^ int 219 ps
--- float operators, local vars
float += float 435 ps
float = float + float 1756 ps
float *= float 873 ps
float = float * float 2264 ps
float %= float 21380 ps
float = float % float 11438 ps
float /= float 1757 ps
float = float / float 8302 ps
--- double operators, local vars
double += double 436 ps
double = double + double 1745 ps
double *= double 1309 ps
double = double * double 2618 ps
double %= double 21506 ps
double = double % double 11378 ps
double /= double 1742 ps
double = double / double 14424 ps
=== .Operator TOTAL: 332610 ps
*** done: 5:10 (tests 5:03) ***

No Comments »

Comment on this entry

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">