Search code examples
file-iorandom-access

Why does reading a file multiple times vary in reading-time?


This is perhaps a very obvious question due simple computer science rules, but is there a good explanation of why it varies so much from time to time? Reading a small file can sometimes take a few milliseconds and other times it can take a few seconds. Of course this depends on how you read the file, and also what language you read it in (i.e the programming language).

Maybe there isn't a obvious answer for this? I'm not sure, I havn't read much about it, so that is why I'm asking the question.


Solution

  • One thing that can cause varying read time is whether the file is in memory or not.

    Disk is a much slower read than from memory. So if a file has been read and placed in memory, it will be much quicker to read from that file afterwards, until it gets kicked out of memory.