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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM