Match integer with String - use string literal or primitive in terms of performance and memory?

Option 1:

String newStr = someStr + 3 + "]"; 

Option 2:

 String newStr = someStr + "3" + "]"; 

Which option is better in terms of performance, memory, and general practice? What recommended tools / methods can I use to measure the memory usage of my code and its performance (besides measuring the start time and end time and calculate the difference)

+8
java string concatenation primitive
source share
5 answers

The first will be:

 StringBuilder sb = new StringBuilder (String.valueOf (someStr)); sb.append (3); sb.append ("]"); String newStr = sb.toString (); 

the second will be:

 StringBuilder sb = new StringBuilder (String.valueOf (someStr)); sb.append ("3"); sb.append ("]"); String newStr = sb.toString (); 

Here's a showdown:

 public String foo (String someStr) { String newStr = someStr + 3 + "]"; return newStr; } public String bar (String someStr) { String newStr = someStr + "3" + "]"; return newStr; } public java.lang.String foo(java.lang.String); Code: 0: new #16 // class java/lang/StringBuilder 3: dup 4: aload_1 5: invokestatic #18 // Method java/lang/String.valueOf:(Ljava/lang/Object;)Ljava/lang/String; 8: invokespecial #24 // Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V 11: iconst_3 12: invokevirtual #27 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder; 15: ldc #31 // String ] 17: invokevirtual #33 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 20: invokevirtual #36 // Method java/lang/StringBuilder.toString:()Ljava/lang/String; 23: astore_2 24: aload_2 25: areturn public java.lang.String bar(java.lang.String); Code: 0: new #16 // class java/lang/StringBuilder 3: dup 4: aload_1 5: invokestatic #18 // Method java/lang/String.valueOf:(Ljava/lang/Object;)Ljava/lang/String; 8: invokespecial #24 // Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V 11: ldc #44 // String 3 13: invokevirtual #33 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 16: ldc #31 // String ] 18: invokevirtual #33 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 21: invokevirtual #36 // Method java/lang/StringBuilder.toString:()Ljava/lang/String; 24: astore_2 25: aload_2 26: areturn 
+10
source share

There will be no noticeable difference between them. Use what you find the most logical and readable. I would use

 String newStr = someStr + "3]"; 
+7
source share

I would recommend Jprofiler as a great Java application profiling tool that helped me find a lot of memory problems.

I do not think that options 1 and 2 have a big difference in terms of memory usage, especially if this applies to a desktop application.

+3
source share

Assuming someString is a constant, both are constant expressions and will be evaluated at compile time. They will result in identical class and runtime files.

Source: Java Language Specification :

A compile-time constant expression is an expression representing a primitive type or String value that does not end abruptly and is composed using only the following:

  • Primitive type literals and String type literals (ยง3.10.1, ยง3.10.2, ยง3.10.3, ยง3.10.4, ยง3.10.5)

  • Additive operators + and - (ยง15.18)

  • ...

String compilation time constant expressions are always interned to exchange unique instances using the String.intern method.

If someString is not a constant, most modern compilers will use StringBuilder, which is explicitly resolved using the Java language specification:

The result of string concatenation is a reference to a String object, which is a concatenation of two operands. The characters of the left operand precede the characters of the right operand in the newly created line.

A String object has just been created (ยง12.5) if the expression is not an expression of a compile-time constant (ยง15.28).

An implementation may decide to perform the conversion and concatenation in one step to avoid creating and then dropping the intermediate String object. To increase string re-concatenation performance, the Java compiler can use the StringBuffer class or a similar method to reduce the number of intermediate String objects created when evaluating an expression.

For primitive types, an implementation can also optimize the creation of a wrapper by converting directly from a primitive type to a string.

+2
source share

Whenever you concatenate a string, at each concatenation you create a new copy of the string, and both strings are copied, one character at a time. This leads to the time complexity of O (n 2 ) (McDowell).

If you want to improve performance, use

 StringBuilder 

One of its constructors has the following syntax:

 public StringBuilder(int size); //Contains no character. Initial capacity of 'size'. 

StringBuilder (a mutable sequence of characters. Remember that strings are immmutable) helps solve this problem by simply creating a mutable array from all strings. copying them back to the string only if necessary (McDowell).

 StringBuilder str = new StringBuilder(0); str.append(someStr); str.append(3); str.append("]"); 

Link:

McDowell, Gail Laakmann. Cracking The Coding Interview, 6th Edition. Print.

"Stringbuilder (Java SE 8 platform)." Docs.oracle.com. Np, 2016. Web. June 4, 2016.

0
source share

All Articles