FileStream.Read()方法C#中的延迟

时间:2013-09-22 16:04:14

标签: c# performance filestream large-data

我在使用FileStream从磁盘(大小为500mb)读取文件时遇到一些性能问题。我目前从磁盘上的文件集一次读取8K字节。但我注意到程序有时会变慢,所以我在FileStream.Read()周围放了一个计时器。 我注意到通常它需要不到1毫秒,但有时需要200毫秒,这是程序严重减慢的地方。

这就是我正在做的事情

const int c11_BufSize = 8*1024;
protected Byte[] m_pBuf = new byte[c11_BufSize];
FileStream m_pFile;

//Every 30sec 
m_pFile.Close();
m_pFile.Dispose();
if (m_sCurrentFile == "File-Ping.ts")
{
     m_sCurrentFile = "File-Pong.ts";
}
m_pFile = File.Open(AppSettings.m_FilePath + m_sCurrentFile, FileMode.Open, FileAccess.Read);

stopwatch.Restart();
NumBytesRead = m_pFile.Read(m_pBuf, 0, c11_BufSize);
stopwatch.Stop();
//Write this data to hardware buffers..
//Application Code.....

当我记录“stopwatch.ElapsedMilliseconds”时,我在日志中注意到以下时间。正常读取()操作确实需要比1毫秒以下,所以我觉得“0”,但偶尔我注意到高值像196,130msec等(我尝试了各种缓冲区大小,8K,80K,1MB但我有同样的较长读取时间记录在所有情况下)

  • 21/09/2013 22:46:22 - PlayerStats:ReadTime- 0
  • 21/09/2013 22:46:22 - PlayerStats:ReadTime- 0
  • 21/09/2013 22:46:22 - PlayerStats:ReadTime- 0
  • 21/09/2013 22:46:22 - PlayerStats:ReadTime- 196

当我减少期间,这是从较小的文件(170mb大小)有效读取我没有看到这个问题。因此,我相信当应用程序从较大的文件中读取时会发生这种情况(要开始我需要从较大的文件中读取高达3TB的文件) 任何人都可以帮助我为什么这么长时间服用?有没有其他方法来改善这一点。 提前致谢 萨姆

0 个答案:

没有答案