Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Talk:ToString() generation

Proposal for new Code Template

Below is a more complete code example to justify my performance assertion in Template 3 proposal.

Using a compound String.format is O20 times slower than StringBuilder, while the single property using of String.format is the same Order as just StringBuilder.

Benchmark Example

public class StringBuilderExample {
	private final Locale locale = Locale.getDefault();
	private final String aString = "aStringValue";
	public static final double π = java.lang.StrictMath.PI;
	private final Double aDouble = 0.499999999999999917;
	private final int aMaxInt = Integer.MAX_VALUE;
	private final int aMinInt = Integer.MAX_VALUE;
	private final boolean isTrue = true;
	private final boolean isFalse = false;
 
    /**
     * To string in a better way.
     *
     * @return the toStringBetter as String
     */
    public String toStringBetter()
    {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(String.format("aString=%s, ",this.aString))
        				.append(String.format("π=%f(%s), ",π,π))
        				.append(String.format("aDouble=%f(%s), ",this.aDouble,this.aDouble))
        				.append(String.format("aMaxInt=%s, ",this.aMaxInt))
        				.append(String.format("aMinInt=%s, ",this.aMinInt))
        				.append(String.format("isTrue=%s, ",this.isTrue))
        				.append(String.format("isFalse=%s ",this.isFalse));
        return String.format(this.locale,"%s [%s %s]",this.getClass().getSimpleName(),super.toString(),stringBuilder.toString());
    }
 
	public String toStringWithFormat() {
		return String.format("StringBuilderExample [locale=%s, aString=%s, aDouble=%s, aMaxInt=%s, aMinInt=%s, isTrue=%s, isFalse=%s]", this.locale, this.aString, this.aDouble, this.aMaxInt,
				this.aMinInt, this.isTrue, this.isFalse);
	}
 
	public String toStringWithBuilder() {
		StringBuilder builder = new StringBuilder();
		builder.append("StringBuilderExample [locale=").append(this.locale).append(", aString=").append(this.aString).append(", aDouble=").append(this.aDouble).append(", aMaxInt=")
				.append(this.aMaxInt).append(", aMinInt=").append(this.aMinInt).append(", isTrue=").append(this.isTrue).append(", isFalse=").append(this.isFalse).append("]");
		return builder.toString();
	}
 
    public static void main(String[] args) {
    	final StringBuilderExample stringBuilderExample = new StringBuilderExample();
    	long start = System.currentTimeMillis();
    	System.out.println(stringBuilderExample.toStringWithBuilder());
    	System.out.println("toStringWithBuilder = "+ (System.currentTimeMillis()-start));
 
    	start = System.currentTimeMillis();
    	System.out.println(stringBuilderExample.toStringWithFormat());
    	System.out.println("toStringWithFormat = "+ (System.currentTimeMillis()-start));
 
    	start = System.currentTimeMillis();
    	System.out.println(stringBuilderExample.toStringBetter());
    	System.out.println("toStringBetter = "+ (System.currentTimeMillis()-start));
	}
}

Results

StringBuilderExample [locale=en_GB, aString=aStringValue, aDouble=0.49999999999999994, aMaxInt=2147483647, aMinInt=2147483647, isTrue=true, isFalse=false]
toStringWithBuilder = 1
StringBuilderExample [locale=en_GB, aString=aStringValue, aDouble=0.49999999999999994, aMaxInt=2147483647, aMinInt=2147483647, isTrue=true, isFalse=false]
toStringWithFormat = 19
StringBuilderExample [com.williamhill.automation.mspamer.StringBuilderExample@1395ddba aString=aStringValue, π=3.141593(3.141592653589793), aDouble=0.500000(0.49999999999999994), aMaxInt=2147483647, aMinInt=2147483647, isTrue=true, isFalse=false ]
toStringBetter = 1

Back to the top