• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

variable declaration

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
what would the difference between these scenario's wrt performance.
notice the declarations of the variables tempid and tempname !
String stArr[][]=new String[j-1][2];
for (int i=1;i<j;i++)
{
int k=i-1;
String tempid=((Vector)(resVec.elementAt(i))).elementAt(0)+"";
String tempname=((Vector)(resVec.elementAt(i))).elementAt(1)+"";
stArr[k][0]=tempid;
stArr[k][1]=tempname;
}

and
String stArr[][]=new String[j-1][2];
String tempid=null;
String tempname=null;
for (int i=1;i<j;i++)
{
int k=i-1;
tempid=((Vector)(resVec.elementAt(i))).elementAt(0)+"";
tempname=((Vector)(resVec.elementAt(i))).elementAt(1)+"";
stArr[k][0]=tempid;
stArr[k][1]=tempname;
}
Thanx
Manoj
:roll:
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the second example would be slightly slower cause you initialize the variables to null. The performance inside the loop is identical.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe that unnecessary initialisation of local variables does indeed slow performance.
Additionally, it reduces the compiler's chance of detecting certain programming errors. For instance, if you declare a variable and initialise it to some arbitrary value (e.g. null, for an object reference), then use it in an expression before you set it to a sensible value, the compiler will not complain. But if you do not initialise it, then use it before setting it, the compiler should notice this and refuse to compile the code.
Two good reasons not to initialise unnecessarily.
As an aside, instance variables and static variables get initialised to well-defined values, if you do not initialise them explicitly. Object references are initialised to null, for instance. Therefore, there is no need to explicitly initialise to these values, and doing so has a small bad effect on performance. I like to do the following:


This way, you are reminded what value will be used for initialisation, but at no performance cost.
[ January 17, 2002: Message edited by: Peter Chase ]
 
Manoj Pandey
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply Sune and Peter. What I understand is that both of you are suggesting that the second way is better, but what if the loop runs say 1000 times, would the declaration of the variables a 1000 times inside the loop not be expensive?? Isnt is less expensive to declare the variables once outside the loop once,set them to null, and and use them .
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Method void test()
0 aconst_null
1 astore_1
2 iconst_0
3 istore_2
4 goto 15
7 iload_2
8 invokestatic #2 <Method java.lang.String valueOf(int)>
11 astore_1
12 iinc 2 1
15 iload_2
16 bipush 10
18 if_icmplt 7
21 return

Method void test()
0 iconst_0
1 istore_1
2 goto 13
5 iload_1
6 invokestatic #2 <Method java.lang.String valueOf(int)>
9 astore_2
10 iinc 1 1
13 iload_1
14 bipush 10
16 if_icmplt 5
19 return
The only difference is the additional aconst_null & astore_1 in the first solution.
So in most applications there won't be any measurable difference between the two solutions, especially as the modern optimizing hotspot engine might even drop the aconst_null, as it isn't really needed here.
Conclusion: Choose the solution that is better readable. In fact you should always follow this advice until really *experiencing* a performance problem. See http://c2.com/cgi/wiki?OptimizeLater
[ January 18, 2002: Message edited by: Ilja Preuss ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic