4

I have following code to write byte array to OutputStream in Java program.

public static void write(OutputStream out, ArrayList<byte[]> buffer) throws IOException {
    for (byte[] packet: buffer) {
        out.write(packet);
    }
}

When buffer size is 770,000 and the max length of each element is 40, this loop takes 2840 millisecond.

Is there a more optimized / faster way of doing that convertion?

4
  • how big are those packets? if your packets are 8 bytes size total then you should build larger packets and write less times Commented Aug 18, 2018 at 18:32
  • 3
    You can try a) wrap the output stream into a buffered stream, or b) if memory is not an issue, copy X small arrays into one large and write large arrays. Commented Aug 18, 2018 at 18:32
  • @MartínZaragoza the OP says "packets" are 40 element max. Commented Aug 18, 2018 at 18:33
  • 1
    Seems fairly fast to me: ideone.com/M8sZhw Convert the array of arrays to a single array and write that to the output stream all in one shot.. 30ms at most. Commented Aug 18, 2018 at 18:58

1 Answer 1

3

Although the use-case is not completely clear - what is the underlying stream, etc, but among a few options you have wrapping the output stream into a buffered one seems to make sense, please see a short example below. On FileOutputStream, with 400k small arrays, it gives 20x performance boost:

// Buffered write, time elapsed: 51
// Raw write, time elapsed: 1050

import java.io.*;
import java.util.*;


public class Test {
    public static void main(String [] args) throws Exception {
        int max = 400000;
        List<byte[]> arrays = new ArrayList<>(max);
        for(int i=0; i< max; i++) {
            arrays.add(new byte[40]);
        }

        try(FileOutputStream fout = new FileOutputStream("/tmp/deleteme")) {
            long start = System.currentTimeMillis();
            writeBuffered(arrays, fout);
            System.out.println("Buffered write, time elapsed: " + (System.currentTimeMillis() - start));
        }
        try(FileOutputStream fout = new FileOutputStream("/tmp/deleteme")) {
            long start = System.currentTimeMillis();
            writeRaw(arrays, fout);
            System.out.println("Raw write, time elapsed: " + (System.currentTimeMillis() - start));
        }
    }

    static void writeRaw(List<byte[]> arrays, OutputStream out) throws IOException {
        for (byte[] packet: arrays) {
            out.write(packet);
        }
    }

    static void writeBuffered(List<byte[]> arrays, OutputStream out) throws IOException {
        BufferedOutputStream bout = new BufferedOutputStream(out);
        for (byte[] packet: arrays) {
            bout.write(packet);
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.