gpt4 book ai didi

java - 为什么我的流复制过程性能稳定下降

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:37:16 24 4
gpt4 key购买 nike

此代码在复制大文件时运行速度越来越慢。我做错了什么吗?

    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);
}

我的 Java 是:

openjdk 10.0.2 2018-07-17 OpenJDK Runtime Environment (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.4)

OpenJDK 64-Bit Server VM (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.4, mixed mode)

最佳答案

您需要在每次比较后重置您的基准“时间”。尝试使用这个:

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");
}

关于java - 为什么我的流复制过程性能稳定下降,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55235988/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com