为什么我的流复制过程性能不断下降

此代码在复制大文件期间工作速度越来越慢。我做错了什么吗?


    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 运行时环境 (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.4)


OpenJDK 64 位服务器 VM (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.4, 混合模式)


慕仙森
浏览 76回答 2
2回答

Smart猫小萌

您需要在每次比较后重置基本“时间”。尝试使用以下命令: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");}

POPMUISE

性能会下降,因为计算错误。对于第二个区块,您正在根据第二个区块大小计算每秒字节数,但从两个区块时间计算。尝试在以下时间后添加time = System.currentTimeMillis();long took = ...
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java