加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-15 04:50:16 所属栏目:Java 来源:网络整理
导读:在复制大文件期间,此代码的工作速度越来越慢.难道我做错了什么? InputStream ms2 = new BufferedInputStream(new FileInputStream("/home/fedd/Videos/homevid.mp4")); OutputStream fos2 = new BufferedOutputStream(new FileOutputStream("testfile2.mp4"
在复制大文件期间,此代码的工作速度越来越慢.难道我做错了什么?

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读