Optimize printing massive strings to System.out with respect to time I need to cut the time it takes to print a large string by ~50%. 

I went from using s1 + s2 to using StringBuilder, and instead of making several calls to System.out.print for each line, I create one large string by appending newlines to the StringBuilder, and then print that large string just once with System.out. 

But my code is still way too slow (this is for a homework assignment and the code is not accepted by the automated hand-in system because it takes too long).

When the StringBuilder is printed, the string has ~100k lines

The relevant part of my code looks like this:

public class Main {
    public static void main (String[] args){
        MyClass builder = new MyClass();
        new ClassThatBuildsOnBuilder(builder);
        System.out.println(builder.str.toString());
    }
}


public class MyClass {
    private double[] coordinates = new double[]{0, 0, 0, 0};

    public StringBuilder str = new StringBuilder();
    private DecimalFormat formatter = new DecimalFormat("#0.0000");

    public MyClass(){

    }

    public void changeValues(double[] coordinates){
        this.coordinates = coordinates;
        updateRow();
    }

    private void updateRow(){
        for (double d : coordinates){
            str.append(formatter.format(d));
            str.append(" ");
        }
        str.append("\n");
    }


Can this even be optimized further? I feel like this should be very close to a lower bound.
Are you sure the bottleneck is the printing, as opposed to whatever new ClassThatBuildsOnBuilder(builder); is doing? 
Is coordinates always going to have a width of four?
It appears you're building the whole string in memory, and then printing all your output at once at the end. While printing each line to standard-out individually may be slower, it also may take better advantages of the built-in output buffering. (Printing each word individually as you describe having originally done probably isn't a great idea.


Would something like this work?

  private void printRow(){
    System.out.format("#%.3f #%.3f #%.3f #%.3f%n",
      coordinates[0],
      coordinates[1],
      coordinates[2],
      coordinates[3]);


You could also try a Formatter, which would let you use a StringBuilder as you already are, but also use printf syntax. 

Finally, I don't know how to take manual control of System.out's buffering behavior, but it's probably possible and may help.