What am trying to do here is to improve speed performance by using StringBuilder outside the loop and clear StringBuilder in each cycle for reuse , but seems like this approach doesn't always work , because I didn't see improvement in speed performance.*
public class Loader implements Runnable {
private int i;
public Loader(int i) {
this.i = i;
}
@Override
public void run() {
long start = System.currentTimeMillis();
try {
PrintWriter writer = new PrintWriter("res/NUMBERS " + i + ".txt");
char letters[] = {'У', 'К', 'Е', 'Н', 'Х', 'В', 'А',
'Р', 'О', 'С', 'М', 'Т'};
StringBuilder stringBuilder = new StringBuilder();
for (int regionCode = 1; regionCode <= 199; regionCode++) {
stringBuilder.setLength(0);
for (int number = 1; number < 1000; number++) {
for (char firstLetter : letters) {
for (char secondLetter : letters) {
for (char thirdLetter : letters) {
stringBuilder.append(firstLetter)
.append(padNumber(number, 3))
.append(secondLetter)
.append(thirdLetter)
.append(padNumber(regionCode, 2))
.append("\n");
}
}
}
}
writer.write(stringBuilder.toString());
}
writer.flush();
writer.close();
System.out.println(System.currentTimeMillis() - start);
} catch (IOException e) {
e.printStackTrace();
}
}
private static String padNumber(int number, int numberLength) {
String numberStr = Integer.toString(number);
int padSize = numberLength - numberStr.length();
for (int i = 0; i < padSize; i++) {
numberStr = '0' + numberStr;
}
return numberStr;
}
public static void main(String[] args) {
for (int i = 0; i < 1; i++) {
new Thread(new Loader(i)).start();
}
}
}
When I used the StringBuilder inside the loop it took 30 sec for the process to finish, but with this approach shown above is taking 50 sec for the process to finish which was not what I expected.Thanks in advance.

setLengthis more time intensive than the StringBuilder's constructor. Granted, you won't see a difference until you start measuring at the nanosecond level, but the idea that you will save time by reusing the same instance of a StringBuilder is faulty.Something like this would be a great candidate for concurrency if your focus is simple speeding up the process.