views:

211

answers:

7

I have an object array, I know that the elements are type String, say I need to access them many times.

  • Practice 1: access the element by array index and cast it to String every time I need it.
  • Practice 2: create local String instances and access each of the elements once.

Which will run faster? If it's on a mobile device where memory is limited, which would be an over all better practice? Thank you.

+19  A: 

You're asking the wrong question. Don't optimize until you know what needs to be optimized. Instead, write the clearest, easiest to understand code you can, then refactor when you know there's an issue (and you've determined what the issue is).

In this case, I'd think it's a lot easier to just maintain an array and cast them to String as needed. If that turns out to be a problem, I'd refactor (possibly by creating a String array and copying the objects into that, once).

Chris B.
+1 for "avoid premature optimisation"
Andrzej Doyle
I disagree, in the mobile world, optimizations like this help battery life.
Ben S
By how much? If writing a complex, difficult to maintain, and buggy optimization saves 1 second of battery power when running over an hour, is that worth it?Once you finish the application, _then_ is the time to see what's running slowly, or draining too much power, or taking too much memory, and optimize _that_.
Chris B.
I also disagree but for a completely different reason - you should always have an enquiring mind about how to create really good code. It's very useful to know which practices are more efficient, so that given two equally maintainable/clear implementation options you choose the right one. That's not premature optimisation, it's just good coding.
mikera
A: 

...

String s = (String)myarray[myelem_id];

does not yield a local string instance but a reference to the existing element -- no copy of the string is created. So the question boils down to: is it better to constantly repeat (String)myarray[myelem_id] or to write it once to initialize local a reference?

The second is easier to read - provided the reference has a meaningful name, i.e. curItem. I doubt there are any significant performance differences in general and I wouldn't benchmark that unless I had really, really strong performance problems (casting is not a zero-time operation because the VM is forced to typecheck, but it isn't slow).

Alexander Gessler
A: 

If you know that you're going to access the same data many times, it would be best to avoid casting and re-casting the Strings.

Ben S
+2  A: 

If you know all the elements are String, why not just create a String array to begin with?

If these are coming from somewhere not under your control, check to see if the class in question supports Generics or not; you may be able to get it to return a String array that way instead.

R. Bemrose
A: 

Assuming you can't make the array a String[] rather than an Object[] ...

If you will loop through once but then use each element many times, I'd write something like ...

for (int x=0; x<whatever.length; ++x)
{
  String whatever1=(String)whatever[x];

Then use whatever1 throughout the body of the loop. This means you do the array lookup and the cast only once rather than repeatedly.

(The same applies if you are not really looping through but using some other means to find the desired entry, but are still using each entry generally only once.)

If you have to process each element of the array many times interspersed with using other elements, it might be beneficial to copy the Object[] to a String[], like:

String[] sWhatever=new String[oWhatever.length];
for (int x=oWhatever.length-1; x>=0; --x)
  sWhatever[x]=(String)oWhatever[x];

(Can you write "System.arraycopy(oWhatever, 0, sWhatever, 0, oWhatever.length)" ? I think that would work, though I haven't tried it.)

Whether the copy would take longer than the casts would depend on how often you have to cast each element. My guess is it would rarely be worth it, but it's possible.

Jay
A: 

If you know that the type is 'always' String then why not call toString() method. It will always give you the actual value...

for (int x=0; x<whatever.length; ++x)
{
    System.out.println(whatever[x].toString());
}
Favonius
A: 

Assuming that you are working on Java ME, with my experience, it all boil downs to

  1. How many elements of the array are accessed?
  2. What is size of original array?

Remember, creating another array, by casting all elements would give negative effect if only few of those are accessed. Also, if you write your code in such a way that one element of array need casting only once, there is no use of casting all at once.

Ashish Patil