Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Vector.size()

 
Chinmay Bajikar
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
My Question is,
Is this code snippet better than the one following it
Snippet 1
Vector v = new Vector();
//add some elems to vector
for (int i=0;i<v.size;i++)
{
//do something
//No v.addElement() or v.removeElement()
}
Snippet 2
Vector v = new Vector();
//add some elems to vector
int siz = v.size();
for (int i=0;i<siz;i++)
{
//do something
//No v.addElement() or v.removeElement()
}
I have heard of something like compiler optimization.
Does'nt the compiler identify this and replace it by constants?
Thanks,
Chinmay.
 
Marilyn de Queiroz
Sheriff
Posts: 9065
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does snippet #1 compile?
 
Chinmay Bajikar
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok Sheriff,I forgot the "()" after v.size.
I have it corrected here.
Sorry for the slip.
Snippet 1 shuld be...
Vector v = new Vector();
//add some elems to vector
for (int i=0;i<v.size();i++)
{
//do something
//No v.addElement() or v.removeElement()
}
 
Vin Kris
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In snippet #1 v.size() is evaluated for every iteration of the loop. In your example, the loop body is not modifying the contents of the vector, but other loops may - so every iteration will compare at a different value. The compiler cannot make the assumption with the Vector that the contents will not change.
[ September 28, 2002: Message edited by: Vin Kris ]
 
Wilfried LAURENT
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to be more efficient, replace
for (int i=0;i<siz;i++)
by
for (int i=siz-1;i>=0;i--)
The comparison to zero is optimized at processor level.
W.
 
Chinmay Bajikar
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,
Thanks all of u for ur replies.
I surely will keep in mind these things will coding next times.
Chinmay........
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vin Kris:
In snippet #1 v.size() is evaluated for every iteration of the loop. In your example, the loop body is not modifying the contents of the vector, but other loops may - so every iteration will compare at a different value. The compiler cannot make the assumption with the Vector that the contents will not change.

On the other hand, Vector.size() is a simple getter and the Hotspot Engine might very well decide to inline the access to the member variable.
I don't think you need to worry about this.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember the first rule of chnaging your code for performance... don't. (At least not until you have identified a performance problem.)
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way to check performance is to test it. So I wrote a little program to test the difference:

For 100,000 loops through a Vector containing 100 entries (10,000,000 loops total) the figures were:
1718 - v.size()
32 - size
So I decided to change it to using an ArrayList instead of a Vector:
47 - v.size()
16 - size
So what have we learned today? Don't use the Vector class!
 
Vin Kris
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A good example Paul. Thanks. The more i learn about Vector, I more I dislike it.
Ila, The decompiled output of the following code -

Method void main(java.lang.String[])
0 new #2 <Class java.util.Vector>
3 dup
4 invokespecial #3 <Method java.util.Vector()>
7 astore_1
8 iconst_0
9 istore_2
10 goto 16
13 iinc 2 1
16 iload_2
17 aload_1
18 invokevirtual #4 <Method int size()>
21 if_icmplt 13
24 return
Step 18 - invokevirtual. This means Vector.size() is not getting inlined, isn't it?. I tested this in 1.4.1-beta.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Thomas Paul:
1718 - v.size()
32 - size
So I decided to change it to using an ArrayList instead of a Vector:
47 - v.size()
16 - size
So what have we learned today? Don't use the Vector class!

Interesting - it's most probably because of the synchronization, isn't it? Mhh, it was my impression that synchronization was getting very cheap in the newer JVMS - which version did you use?
[ October 02, 2002: Message edited by: Ilja Preuss ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vin Kris:
Step 18 - invokevirtual. This means Vector.size() is not getting inlined, isn't it?. I tested this in 1.4.1-beta.

Not yet. Remember that there is another compilation step involved in Java - at runtime, when the Hotspot Engine compiles parts of the bytecode to native code. I think it's more than likely that the method call gets inlined then, if it gets called often enough.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic