java - Which code snippet executes faster? -


i can same thing 2 types of code snippet.

first way:

string makedate = integer.tostring(now.year) + integer.tostring(now.month) + integer.tostring(now.monthday); 

or second way:

string makedate = now.year + "" + now.month + "" + now.monthday; 

my question is:

  1. which method preferable [first way or second way]?
  2. which code snippet execute faster?

the 2 snippits show identical.

a string in java immutable; can't changed. when using concatenation operator (+) compiler generates code using stringbuilder

for example second snippit becomes:

string makedate = new stringbuilder()                    .append(now.year)                    .append("")                    .append(now.month)                    .append("")                    .append(now.monthday)                    .tostring(); 

you can @ generated bytecode see this. java comes program javap allows @ compiled .class.

i created simple main() provide minimal bytecode:

public static void main(string[] args)  {     string makedate = integer.tostring(1) + integer.tostring(1) + integer.tostring(1);     system.out.println(makedate); } 

which produces:

public static void main(java.lang.string[]);     flags: acc_public, acc_static     code:       stack=2, locals=2, args_size=1          0: new           #2                  // class java/lang/stringbuilder          3: dup                     4: invokespecial #3                  // method java/lang/stringbuilder."<init>":()v          7: iconst_1                8: invokestatic  #4                  // method java/lang/integer.tostring:(i)ljava/lang/string;         11: invokevirtual #5                  // method java/lang/stringbuilder.append:(ljava/lang/string;)ljava/lang/stringbuilder;         14: iconst_1               15: invokestatic  #4                  // method java/lang/integer.tostring:(i)ljava/lang/string;         18: invokevirtual #5                  // method java/lang/stringbuilder.append:(ljava/lang/string;)ljava/lang/stringbuilder;         21: iconst_1               22: invokestatic  #4                  // method java/lang/integer.tostring:(i)ljava/lang/string;         25: invokevirtual #5                  // method java/lang/stringbuilder.append:(ljava/lang/string;)ljava/lang/stringbuilder;         28: invokevirtual #6                  // method java/lang/stringbuilder.tostring:()ljava/lang/string;         31: astore_1               32: getstatic     #7                  // field java/lang/system.out:ljava/io/printstream;         35: aload_1                36: invokevirtual #8                  // method java/io/printstream.println:(ljava/lang/string;)v         39: return      

versus:

public static void main(string[] args)  {     int = 1;     string makedate = + "" + + "" + i;     system.out.println(makedate); } 

produces:

public static void main(java.lang.string[]);     flags: acc_public, acc_static     code:       stack=2, locals=3, args_size=1          0: iconst_1                1: istore_1                2: new           #2                  // class java/lang/stringbuilder          5: dup                     6: invokespecial #3                  // method java/lang/stringbuilder."<init>":()v          9: iload_1                10: invokevirtual #4                  // method java/lang/stringbuilder.append:(i)ljava/lang/stringbuilder;         13: ldc           #5                  // string          15: invokevirtual #6                  // method java/lang/stringbuilder.append:(ljava/lang/string;)ljava/lang/stringbuilder;         18: iload_1                19: invokevirtual #4                  // method java/lang/stringbuilder.append:(i)ljava/lang/stringbuilder;         22: ldc           #5                  // string          24: invokevirtual #6                  // method java/lang/stringbuilder.append:(ljava/lang/string;)ljava/lang/stringbuilder;         27: iload_1                28: invokevirtual #4                  // method java/lang/stringbuilder.append:(i)ljava/lang/stringbuilder;         31: invokevirtual #7                  // method java/lang/stringbuilder.tostring:()ljava/lang/string;         34: astore_2               35: getstatic     #8                  // field java/lang/system.out:ljava/io/printstream;         38: aload_2                39: invokevirtual #9                  // method java/io/printstream.println:(ljava/lang/string;)v         42: return         

technically latter faster @ scale immeasurable (< 1ns) practical purposes doesn't matter; use whichever like.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -