vm optimizations

From: Murat Tasan (tasan_at_eecs.cwru.edu)
Date: 10/30/03


Date: Wed, 29 Oct 2003 22:46:04 -0500

i asked something about java optimization before, but i gave a very poor
example... leading to not much help... so here is a better situation.

i have a Map, and i'm constantly picking entries from random from this
map.

in one method, i only use the map.
in another method, i use an array of keys from the map, stored seperately.

method a (only the map):

Map myMap = //initialize map
Object randObj;
// begin loop
randObj = myMap.keySet().toArray()[(int)Math.floor(Math.random() *
myMap.size())];

so, as you can see, in method a i constantly re-acquire the array of
keys... but reduce the clutter.

method b:

Map myMap = //initialize map
Object[] keyArray = myMap.keySet().toArray();
// begin loop
randObj = myMap.get(keyArray[(int)Math.floor(Math.random() *
keyArray.length)]);

the only change then is one single array that has a name.

what i'm wondering is:

does the compiler or runtime environment do any optimizations such that
method a is the same (or better maybe) than method b?

while i know the call stack will be one shorter in method b, i'm really
curious about whether or not method a constantly allocates and frees up
memory for the array.

does anyone know where i can find out about these sorts of issues, or can
anyone clear this example out for me?

thanks much,

murat

-- 
Murat Tasan
mxt6@po.cwru.edu
tasan@eecs.cwru.edu
murat.tasan@cwru.edu
http://genomics.cwru.edu


Relevant Pages

  • Re: Can arrays be parameters to generics
    ... may be less than 500 entrys one won't notice it that much. ... possess Set or Map to rocket, ... But sometimes an array can replace those. ... government people -- they wear both hats. ...
    (comp.lang.java.programmer)
  • java.lang.NullPointerException..help me!
    ... public UploadedFile getMyFile() { ... // Populate the map from the input file. ... String line = null; ... // Create the array for the numbers which make up the rest of the line. ...
    (comp.lang.java.programmer)
  • Error setting property myFile in bean of type null
    ... My Jsf Application allow user to upload a txt file thanks to a Myfaces ... // Populate the map from the input file. ... String line = null; ... // Populate the array by parsing the rest of the line. ...
    (comp.lang.java.programmer)
  • Re: Readline using foreach and while
    ... condition is checked at the start of each iteration, if the array ... map doesn't recheck the count ... the passed list for each iteration. ... @ary, which comes from a scope outside this sub, now contains (h, a, b, ...
    (comp.lang.perl.misc)
  • Error setting property myFile in bean of type null
    ... My Jsf Application allow user to upload a txt file thanks to a Myfaces ... // Populate the map from the input file. ... String line = null; ... // Populate the array by parsing the rest of the line. ...
    (comp.lang.java.help)