On Wednesday 18 July 2007 02:58, Andreas L Delmelle wrote: > On Jul 16, 2007, at 22:25, J.Pietschmann wrote: > > Vincent Hennebert wrote: > >>> Addition of a general-purpose int-to-int map ... > > > > ... > > > As to the efficiency: > I did some measurements of the difference in processing speed (for > 64K elements), and the factor lies somewhere between 5 and 20 (times > faster). A difference that is not caused by the HashMap lookups, but > almost solely due to the necessary Integer constructions and casts... > > So, the motives were more aesthetic, I guess. More caused by my > aversion for the dumb Integer immutables... I have nothing against > HashMaps, but I just do not care much for lines of code like this: > > int someInt = ((Integer) someMap.get(new Integer > (anotherInt))).intValue(); > > Granted, CPUs have become so fast and JVMs have been optimized in > such a way that one does not even notice the difference unless when > executing this ugly line of code 10 million times in a row, but > still... >
Interestingly Java 1.5 has added the Integer.valueOf(int) method with the following comment: Returns a Integer instance representing the specified int value. If a new Integer instance is not required, this method should generally be used in preference to the constructor Integer(int), as this method is likely to yield significantly better space and time performance by caching frequently requested values. Obviously we can't use it because of backwards compatibility with 1.4 but it seems to address, to some extent, the performance issue you tried to solve. > > Cheers > > Andreas Cheers Manuel