Performance Tip: Avoid using String + inside a StringBuilder.append()

Overview

It can make sense to use String + or a StringBuilder however using them in combination is both inefficient and confusing. IMHO make the decision to use one or the other.

From VMId.toString()
result.append((x < 0x10 ? "0" : "") +
          Integer.toString(x, 16));
Here a '0' is optionally added to the start of a number. This could be written as
result.append((x < 0x10 ? "0" : ""));
result.append(Integer.toString(x, 16));
However using an if statement is likely to be clearer if not at least faster
if (x < 0x10) result.append('0');
result.append(Integer.toString(x, 16));

Comments

Popular posts from this blog

Why You Should Tune Code Before Your Garbage Collector

Asking multiple AI to optimise the same code

Which Doc Format is Best for AI Specifications?