在复制大文件期间,此代码的运行速度越来越慢。难道我做错了什么?
InputStream ms2 = new BufferedInputStream(new FileInputStream("/home/fedd/Videos/homevid.mp4"));
OutputStream fos2 = new BufferedOutputStream(new FileOutputStream("testfile2.mp4", true));
try {
int byt;
int i = 0;
long time = System.currentTimeMillis();
while ((byt = ms2.read()) != -1) {
fos2.write(byt);
i++;
if (i > 100000) {
i = 0;
long took = System.currentTimeMillis() - time;
System.out.println("100000 bytes took " + took + " milliseconds which means " + (100000000 / took) + " bytes per second");
}
}
fos2.close();
ms2.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
Run Code Online (Sandbox Code Playgroud)
我的Java是:
openjdk 10.0.2 2018-07-17 OpenJDK运行时环境(内部版本10.0.2 + 13-Ubuntu-1ubuntu0.18.04.4)
OpenJDK 64位服务器VM(内部版本10.0.2 + 13-Ubuntu-1ubuntu0.18.04.4,混合模式)
每次比较后,您都需要重置基准“时间”。尝试使用此:
if (i > 100000) {
i = 0;
long took = System.currentTimeMillis() - time;
time = System.currentTimeMillis();
System.out.println("100000 bytes took " + took + " milliseconds which means " + (100000000 / took) + " bytes per second");
}
Run Code Online (Sandbox Code Playgroud)
由于计算错误,因此性能会下降。对于第二个块,您正在从第二个块的大小(但从两个块的时间开始)计算每秒的字节数。尝试time = System.currentTimeMillis();之后添加long took = ...
| 归档时间: |
|
| 查看次数: |
73 次 |
| 最近记录: |