Re: Objects are slow ?
AndrewMcDonagh wrote On 08/03/06 18:01,:
Sure, me too (coming from a background of real-time application and
embedded apps), but I measure the program first, then I perform what
might initially appear to be the micro-optimization.
Let's spend a few moments estimating how "micro" the
optimization is. To summarize the O.P., there's a "huge"
array (apparently two-dimensional) of objects each with a
payload of two ints and two booleans. The O.P. wonders
whether he'd be better off using four parallel arrays of
primitives.
That's not much information to go on, so we'll need to
augment it with some assumptions. I'll assume
- The arrays are [N][N] in size, where N is sufficiently
large for N*N to be called "huge."
- An instance of the O.P.'s object occupies 24 bytes:
ten bytes of payload plus eight bytes of bookkeeping
used by the JVM, rounded up to a multiple of eight.
- An array occupies 16 bytes plus the space for its
payload elements: an array is an object and thus has
the JVM bookkeeping, and an array also probably has
some bookkeeping of its own (e.g., its length is
probably stored somewhere).
- An object reference occupies four bytes.
The latter three assumptions obviously depend on the JVM
and will vary from one to another; if you've got more specific
information about the O.P.'s JVM, by all means use it.
With these assumptions, the SomeClass[N][N] approach uses
- 24*N*N bytes for the SomeClass object instances, plus
- N*(4*N+16) bytes for N one-dimensional arrays containing
N SomeClass references apiece, plus
- 4*N+16 bytes for one one-dimensional array containing N
references to the other arrays.
Grand total for SomeClass[N][N]: 28*N*N + 20*N + 16 bytes.
The alternative of two int[N][N] arrays and two boolean[N][N]
arrays uses
- 2*N*(4*N+16) bytes for two sets of N int[N] arrays, plus
- 2*(4*N+16) bytes for two N-element arrays containing
references to the int[N] arrays, plus
- 2*N*(N+16) bytes for two sets of N boolean[N] arrays, plus
- 2*(4*N+16) bytes for two N-element arrays containing
references to the boolean[N] arrays.
Grand total for parallel arrays: 10*N*N + 80*N + 32 bytes.
Memory saved by using parallel arrays: 18*N*N - 60*N - 16
bytes -- roughly speaking, 18*"huge" bytes. A savings of
18*"huge" bytes isn't a "micro"-optimization.
This isn't to say that the optimization *should* be done,
of course. But the size of the potential savings is large
enough to influence the design from the outset: You might
decide to start with SomeClass[N][N], but if you think you may
want to change to parallel arrays or to a single long[N][N] or
even to a byte[HUGE], you won't let those SomeClass objects
escape "into the wild." You'll hide them behind accessors so
you can change representations easily if it proves necessary.
This is the sort of choice that's easy to make before coding,
much harder to make after the code is deployed and found to
be performing poorly.
--
Eric.Sosman@sun.com