你的测试数据大约是 1GB,压缩到 100MB。根据您的硬件,可能无法实现 < 5s 的性能。
我整理了一个快速而肮脏的基准,突出了写入 zip 文件的性能影响。
- 写入 CSV
String.join()
:9.6 秒
- 使用 Super CSV 写入 CSV:12.7 秒
- 在 zip 中写入 CSV
String.join()
:18.6 秒
- 使用 Super CSV 在 zip 中写入 CSV:22.5 秒
使用 Super CSV (~122%) 似乎有一点开销,但无论是否使用 Super CSV,仅写入 zip 文件的时间几乎翻了一番 (~190%)。
这是 4 个场景的代码。
与您提供的代码不同,我直接写入文件(我没有注意到写入磁盘与写入内存之间有任何区别,即ByteArrayOutputStream
)。我也跳过了BufferedWriter
Super CSV 示例,因为它已经在内部使用了它,并且我使用了 try-with-resources 来使事情变得更干净。
@Test
public void testWriteToCsvFileWithSuperCSV() throws Exception {
long startTime = System.currentTimeMillis();
try (FileOutputStream csvFile = new FileOutputStream(new File("supercsv.csv"));
ICsvListWriter writer = new CsvListWriter(new OutputStreamWriter(csvFile, "UTF-8"), CsvPreference.EXCEL_PREFERENCE)
){
for (int rowIdx = 0; rowIdx < 7000000; rowIdx++) {
final List<String> rowContent = new LinkedList<>();
for (int colIdx = 0; colIdx < 6; colIdx++) {
String str = "R" + rowIdx + "C" + colIdx + " FieldContent";
rowContent.add(str);
}
writer.write(rowContent);
}
}
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
System.out.println("Writing to CSV with Super CSV took " + (elapsedTime / 1000f) + " seconds");
}
@Test
public void testWriteToCsvFileWithinZipWithSuperCSV() throws Exception {
long startTime = System.currentTimeMillis();
try (FileOutputStream zipFile = new FileOutputStream(new File("supercsv.zip"));
ZipOutputStream zos = new ZipOutputStream(zipFile);
ICsvListWriter writer = new CsvListWriter(new OutputStreamWriter(zos, "UTF-8"), CsvPreference.EXCEL_PREFERENCE)
){
ZipEntry csvFile = new ZipEntry("supercsvwithinzip.csv");
zos.putNextEntry(csvFile);
for (int rowIdx = 0; rowIdx < 7000000; rowIdx++) {
final List<String> rowContent = new LinkedList<>();
for (int colIdx = 0; colIdx < 6; colIdx++) {
String str = "R" + rowIdx + "C" + colIdx + " FieldContent";
rowContent.add(str);
}
writer.write(rowContent);
}
}
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
System.out.println("Writing to CSV within zip file with Super CSV took " + (elapsedTime / 1000f) + " seconds");
}
@Test
public void testWriteToCsvFileWithStringJoin() throws Exception {
long startTime = System.currentTimeMillis();
try (FileOutputStream textFile = new FileOutputStream(new File("join.csv"));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(textFile, "UTF-8"));
){
for (int rowIdx = 0; rowIdx < 7000000; rowIdx++) {
final List<String> rowContent = new LinkedList<>();
for (int colIdx = 0; colIdx < 6; colIdx++) {
String str = "R" + rowIdx + "C" + colIdx + " FieldContent";
rowContent.add(str);
}
writer.append(String.join(",", rowContent) + "\n");
}
}
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
System.out.println("Writing to CSV with String.join() took " + (elapsedTime / 1000f) + " seconds");
}
@Test
public void testWriteToCsvFileWithinZipWithStringJoin() throws Exception {
long startTime = System.currentTimeMillis();
try (FileOutputStream zipFile = new FileOutputStream(new File("join.zip"));
ZipOutputStream zos = new ZipOutputStream(zipFile);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(zos, "UTF-8"));
){
ZipEntry csvFile = new ZipEntry("joinwithinzip.csv");
zos.putNextEntry(csvFile);
for (int rowIdx = 0; rowIdx < 7000000; rowIdx++) {
final List<String> rowContent = new LinkedList<>();
for (int colIdx = 0; colIdx < 6; colIdx++) {
String str = "R" + rowIdx + "C" + colIdx + " FieldContent";
rowContent.add(str);
}
writer.append(String.join(",", rowContent) + "\n");
}
}
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
System.out.println("Writing to CSV within zip with String.join() took " + (elapsedTime / 1000f) + " seconds");
}