我想知道是否有人可以提供任何建议来提高旧企业 Android 5.1 设备上二进制数据写入的性能。
FileOutputStream fileOutputStream = new FileOutputStream(fileDescriptor);
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
byte[] abData = new byte[1024*1024];
long lStart = System.currentTimeMillis();
for(int i= 0; i < 30; i++){
bos.write(abData);
}
long lDuration = System.currentTimeMillis() - lStart;
utility.logDebug("OEDebug: duration (MS): " + lDuration);
执行上述测试代码写入30MB数据大约需要1.2秒。如果直接使用 FileOutputStream 而不是 BufferedOutputStream,结果实际上是相同的。我们处理的文件大小为数百 MB。每篇文章的编写时间都超过 10 秒。有什么我可以尝试提高速度的吗?
由于硬件限制和操作系统的老化,提高旧版 Android 5.1 设备上二进制数据写入的性能可能具有挑战性。但是,您可以尝试以下几种策略来优化写作过程:
FileOutputStream fileOutputStream = null;
BufferedOutputStream bos = null;
try {
fileOutputStream = new FileOutputStream(fileDescriptor);
// Increased buffer size for performance improvement
int bufferSize = 8 * 1024 * 1024; // 8 MB
bos = new BufferedOutputStream(fileOutputStream, bufferSize);
byte[] abData = new byte[1024 * 1024]; // 1 MB
long lStart = System.currentTimeMillis();
for (int i = 0; i < 30; i++) {
bos.write(abData);
}
long lDuration = System.currentTimeMillis() - lStart;
utility.logDebug("OEDebug: duration (MS): " + lDuration);
} catch (IOException e) {
// Handle exceptions appropriately
e.printStackTrace();
} finally {
try {
if (bos != null) {
bos.close();
}
if (fileOutputStream != null) {
fileOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
备注: