开发者

Why file read is faster on reading again?

SIZE = 1<<16
def justread(file):
    with open(file, 'rb') as f:
        while f.read(SIZE):
            pass

The first ti开发者_如何学Cme I run this function on a 700MB file it took 19 secs. When I repeated

reading the same file again the time it took dropped to 0.5secs.

I repeated this with many files and the results were similar.

What is going on here?


The operating system caches the file in memory upon the first read, and the second time it is read from memory instead of from hard disk. Of course memory is much faster.


Your operating system is caching the file.

What? This means, the first time you read the file, your program had to go look for the information in the hard drive, and hard drives are slooooow.

Now, after having read the whole file for the first time, Linux kept it around in memory just in case, and probably because there was nothing important to have instead at the time. So, the second time you read it, you were accessing the copy in memory, without reaching for the hard drive, which is orders of magnitude faster.

Cheers!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜