zoukankan      html  css  js  c++  java
  • C# FileStream 读取大文件时ReadByte和Read的速度对比

    写了段代码,对比分别用FileStream 的ReadByte和Read读取同一个文件的速度,代码中除了必要读取代码外没有其他业务代码,具体如下

        class Program {
            static void Main(string[] args) {
                Stopwatch sw = new Stopwatch();
                sw.Start();
                Test2();
                sw.Stop();
                Console.WriteLine(sw.ElapsedMilliseconds);
                sw.Restart();
                Console.WriteLine("=============");
                Test1();
                sw.Stop();
                Console.WriteLine(sw.ElapsedMilliseconds);
                Console.ReadKey();
            }
            public static void Test1() {
                using (FileStream fs = new FileStream("2020-05-24.log", FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
                    byte val = 0;
                    long total = fs.Length;
                    int read = 0;
                    while (read < total) {
                        val = (byte)fs.ReadByte();
                        read++;
                    }
                }
            }
            public static void Test2() {
                using (FileStream fs = new FileStream("2020-05-24.log", FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
                    byte[] buffer = new byte[1024 * 1024];
                    int b = 1;
                    while (b > 0) {
                        b = fs.Read(buffer, 0, buffer.Length);
                    }
                }
            }
        }

    第一次,先执行Test1函数执行ReadByte操作,再执行Test2执行Read,发现Read是ReadByte的近20倍

    由于可能硬盘本身读取存在缓存机制,我又是连续读取2次,所以我调换了一下执行顺序

    第二次,先Test2,再Test1,发现Read依然比ReadByte快,但是明显低于第一次测试的读取速度,猜测是硬盘读取的缓存机制导致的

    最终得到结果,取他们都作为第一次读取的速度,发现,通过Read读取数据填充缓冲区明显比ReadByte一个一个的读取快很多,快了近10倍

    而且硬盘读取确实有缓存机制,后读取明显比先读取快很多

    后来我发现Test1是把每个字节都过了一遍,Test2则不是,这显然会严重影响对比速度,所以我就改了下代码又试了下,尽量让他们逻辑一致,发现依然是Read比ReadByte块,只不过没有10倍这么夸张了,经过反复测试,只是快了1倍

    class Program {
            static void Main(string[] args) {
                Stopwatch sw = new Stopwatch();
                sw.Start();
                Test2();
                sw.Stop();
                Console.WriteLine(sw.ElapsedMilliseconds);
                Console.ReadKey();
            }
            public static void Test1() {
                using (FileStream fs = new FileStream("2020-05-24.log", FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
                    byte val = 0;
                    long total = fs.Length;
                    int read = 0;
                    while (read < total) {
                        val = (byte)fs.ReadByte();
                        read++;
                    }
                }
            }
            public static void Test2() {
                using (FileStream fs = new FileStream("2020-05-24.log", FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
                    byte[] buffer = new byte[1024 * 1024];
                    int b = 1;
                    byte val = 0;
                    while (b > 0) {
                        b = fs.Read(buffer, 0, buffer.Length);
                        for (int i = 0; i < b; i++) {  //和Test1一样,逐字节过一遍
                            val = buffer[i];
                        }
                    }
                }
            }
        }

  • 相关阅读:
    opencv出现问题:/usr/lib/x86_64-linux-gnu/libpng16.so.16: undefined reference to `inflateValidate@ZLIB_1.2.9'
    SCI-HUB 解锁论文的正确姿势——如何免费下载论文
    VS2019 实现与 Linux服务器同步
    VS Code 写代码实时同步服务器【Sftp插件】
    Docker实用技巧(二):容器与主机之间复制文件
    Docker实用技巧(一):镜像的备份/保存/加载/删除
    mycat注解及高可用(三)
    mycat分片及主从(二)
    SVN中trunk,branches,tags用法详解
    寄存器的英文全称
  • 原文地址:https://www.cnblogs.com/luludongxu/p/13032762.html
Copyright © 2011-2022 走看看