FileStream.Seek主场迎战雷丁缓冲 [英] FileStream.Seek vs. Buffered Reading

查看:115
本文介绍了FileStream.Seek主场迎战雷丁缓冲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这回答我想知道这是怎么回事下帷幕如果使用大量的FileStream.Seek(-1)

Motivated by this answer I was wondering what's going on under the curtain if one uses lots of FileStream.Seek(-1).

为了清楚起见,我会重新发布了答案:用

For clarity I'll repost the answer:

using (var fs = File.OpenRead(filePath))
{
    fs.Seek(0, SeekOrigin.End);

    int newLines = 0;
    while (newLines < 3)
    {
        fs.Seek(-1, SeekOrigin.Current);
        newLines += fs.ReadByte() == 13 ? 1 : 0; // look for \r
        fs.Seek(-1, SeekOrigin.Current);
    }

    byte[] data = new byte[fs.Length - fs.Position];
    fs.Read(data, 0, data.Length);
}



我个人看过像2048字节到缓冲区,并搜索到缓冲区炭。

Personally I would have read like 2048 bytes into a buffer and searched that buffer for the char.

使用反射器,我发现,在内部的方法是使用的 SetFilePointer

Using Reflector I found out that internally the method is using SetFilePointer.

是否有关于Windows缓存和向后读取文件的任何文档? Windows能否缓冲区倒退,并咨询缓冲区连续使用时,寻求(-1)还是会预读从当前位置开始?

Is there any documentation about windows caching and reading a file backwards? Does Windows buffer "backwards" and consult the buffer when using consecutive Seek(-1) or will it read ahead starting from the current position?

有趣的是,一方面是多数人同意的Windows做的好缓存,但另一方面每一个答案为读取文件时向后需要读取的字节块,并在该块运行。

It's interesting that on the one hand most people agree with Windows doing good caching, but on the other hand every answer to "reading file backwards" involves reading chunks of bytes and operating on that chunk.

推荐答案

今后VS向后通常不会太大的差别。该文件中的数据读入第一次读取后,文件系统缓存,你会得到一个ReadByte内存到内存复制()。只要数据是在高速缓存该副本不是该文件指针值敏感。缓存算法不但是从你通常顺序读取的假设工作。它试图超前读,只要文件扇区仍然是相同的轨道上。他们通常是,除非磁盘严重碎片化。

Going forward vs backward doesn't usually make much difference. The file data is read into the file system cache after the first read, you get a memory-to-memory copy on ReadByte(). That copy isn't sensitive to the file pointer value as long as the data is in the cache. The caching algorithm does however work from the assumption that you'd normally read sequentially. It tries to read ahead, as long as the file sectors are still on the same track. They usually are, unless the disk is heavily fragmented.

但是,是的,它是低效的。你会得到打两的PInvoke和API调用的每个字节。有在开销相当数量的,那些相同的两个电话也能读,说,65千字节用相同量的开销。像往常一样,解决这个问题只有当你发现它是一个PERF的瓶颈。

But yes, it is inefficient. You'll get hit with two pinvoke and API calls for each individual byte. There's a fair amount of overhead in that, those same two calls could also read, say, 65 kilobytes with the same amount of overhead. As usual, fix this only when you find it to be a perf bottleneck.

这篇关于FileStream.Seek主场迎战雷丁缓冲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆