John Doe
John Doe

Reputation: 95

Comparing time taken to read() from file system

I have created a program that measures that time taken for a read() to be performed on a file and I do this several times to determine the block size of my file system.

My question: After plotting this data, everytime I try it, no matter the size I am reading in each iteration, the first read takes significantly longer time compared to any other read. I know that once a block has completed reading, the time to do the next read in the new block will take a bit more time (which I have observed in my plot) but this first read value is much higher than that too.

Does anyone have a filesystems/O.S. based answer to why this is the case?

Upvotes: 0

Views: 192

Answers (1)

Cameron Spence
Cameron Spence

Reputation: 32

I can think of a couple of reasons why this might be the case. The file system might cache (pre-fetch) the data read from disk, so that even if it only returns (say) 1 block to your program, it might have actually read multiple blocks from the disk; so that the next time you do a read, you're actually just pulling more from that cached data. It's also perhaps possible that doing the first read might involve the read head having to move to the start of the file? This is probably very file-system-dependent. I think that cacheing is more likely to be the cause?

Upvotes: 2

Related Questions