Why file read is faster on reading again?

后端 未结 2 714
青春惊慌失措
青春惊慌失措 2021-01-07 04:38

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

The first time I run t

2条回答
  •  太阳男子
    2021-01-07 05:43

    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.

提交回复
热议问题