File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Vector.size() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Vector.size()" Watch "Vector.size()" New topic
Author

Vector.size()

Chinmay Bajikar
Ranch Hand

Joined: Dec 08, 2001
Posts: 159
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.


The strength of the Wolf is the pack & the strength of the pack is the wolf....Rudyard Kipling
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
Does snippet #1 compile?


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Chinmay Bajikar
Ranch Hand

Joined: Dec 08, 2001
Posts: 159
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

Joined: Jun 17, 2002
Posts: 154
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

Joined: Jul 13, 2001
Posts: 269
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

Joined: Dec 08, 2001
Posts: 159
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

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Remember the first rule of chnaging your code for performance... don't. (At least not until you have identified a performance problem.)


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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

Joined: Jun 17, 2002
Posts: 154
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

Joined: Jul 11, 2001
Posts: 14112
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

Joined: Jul 11, 2001
Posts: 14112
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Vector.size()