• Post Reply Bookmark Topic Watch Topic
  • New Topic

doubt in basics of arrays  RSS feed

 
Shrinath M Aithal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
consider this from "Android" documentation :
A somewhat more radical idea is to slice up multidimensional arrays into parallel single one-dimension arrays:

An array of ints is a much better than an array of Integers, but this also generalizes to the fact that two parallel arrays of ints are also a lot more efficient than an array of (int,int) objects. The same goes for any combination of primitive types.
If you need to implement a container that stores tuples of (Foo,Bar) objects, try to remember that two parallel Foo[] and Bar[] arrays are generally much better than a single array of custom (Foo,Bar) objects. (The exception to this, of course, is when you're designing an API for other code to access; in those cases, it's usually better to trade correct API design for a small hit in speed. But in your own internal code, you should try and be as efficient as possible.)


I got this from here : Android docs

Could you please help me understanding that 2 single arrays is better than one 2d array ??
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A "2D array" in Java is always actually a 1D array of 1D arrays -- i.e., an int[10][2] is one array of length 10 that holds ten arrays of 2 ints each; this is total of 11 objects. On the other hand, you could hold the same ints in two 1D int[10]s, which would be only 2 objects. For a tiny VM where object count really mattered -- and for some Android devices this is the case according to the author of the document you link to -- then this would make the 2 1D arrays a better choice.

But for general Java development, it's terrible advice, as is much of what's in that document. In general, don't use that advice except to squeeze additional performance out of troublesome Android applications.
 
Shrinath M Aithal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok... Now thats easy to understand.. Thanks for that Sherif


Ernest Friedman-Hill wrote:
But for general Java development, it's terrible advice, as is much of what's in that document. In general, don't use that advice except to squeeze additional performance out of troublesome Android applications.


Why is that bad as of Java development? Isn't it true that creating objects hurt the time and space in Java too ?? Do you say that because its a bad programming practice, it doesn't look good and structured and so on?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Despite what it says in that article, creating small, short-lived objects in a modern JVM is very close to free. Java's allocator/garbage collector combo is many times faster than C++'s new/delete, for example. Although those tips might speed up a regular Java program by 1% or less, and might cause it to use a few percent less memory, you'll have done so at the expense of creating code that's much harder to understand -- which means it's more likely to contain bugs, which will be hard to find since the code is hard to understand...
 
Shrinath M Aithal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okkk.... So, Android uses dalvik virtual machine, instead of jvm and that is the reason for all those things.. May be that 1% matters a lot on mobile devices with so less resources to spend..
So you say its best to follow what that document has to say about performance on android devices or any device with lesser resources...

Thanks Ernest

Resolved..
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!