which of the two practices is more efficient in Java?
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 instance开发者_如何学Cs 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.
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).
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.
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.
...
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).
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.
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());
}
Assuming that you are working on Java ME, with my experience, it all boil downs to
- How many elements of the array are accessed?
- 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.
精彩评论