java - Is there a performance issue using a temporary variable? -
i have question creating new object in java.
lets have methos called: foo(string[] a)
i want pass foo
new string[]
thats 1 use better
performance 1 or 2
1.
string[] = new string[]{"a"}; foo(a);
2.
foo(new string[]{"a"});
thanks helping
you can check bytecode:
1.
public static void main(java.lang.string[]); code: 0: iconst_0 1: anewarray #2 // class java/lang/string 4: astore_1 5: aload_1 6: invokestatic #3 // method foo:([ljava/lang/string;)v 9: return
2.
public static void main(java.lang.string[]); code: 0: iconst_0 1: anewarray #2 // class java/lang/string 4: invokestatic #3 // method foo:([ljava/lang/string;)v 7: return
the difference in (2), no references stored (astore_1
) , none need loaded (aload_1
). not cause performance difference (and shouldn't worry such minuscule optimizations anyway), might use option (2) if you're not going reference a
again in program (as mentioned in your comment) and there no readability infringement.
i think readability component quite important: rather create variable store array (even if i'll never reference variable more once) have 1 super-long, unreadable line. of course, in example, there no issue readability, it's consider when more complicated situations arise.
Comments
Post a Comment