MemoryExtensions icon indicating copy to clipboard operation
MemoryExtensions copied to clipboard

请教在二进制文件读取中的优化

Open xibaoning opened this issue 4 years ago • 0 comments

看了文章与代码,还是没能悟出在二进制文件读取中使用BufferReader等结合优化问题, 场景如下: 本地读取成百上千的二进制数据文件, 文件大小几KB到几十MB不等, 单进程,同时刻只读取一个文件, 常规方式如下, 感觉有很大的优化空间, 请指教 using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read)){ byte[] buffer = new byte[fs.Length]; //加载到内存再读取比直接读取FileStream快 using (var ms = new MemoryStream(buffer)){ using (BinaryReader reader = new BinaryReader(ms)){ while (fs.Position < filelength){ k = new KData { Open = (float)reader.ReadDouble(), High = (float)reader.ReadDouble(), Low = (float)reader.ReadDouble(), Close = (float)reader.ReadDouble(), Volume = reader.ReadInt32(), Amount = (float)reader.ReadDouble(), }; list.Add(k); } } } }

xibaoning avatar Apr 22 '22 04:04 xibaoning