Wednesday, July 18, 2007

don't abuse OS's cache

I was writing a simple program, open a file, read the data. Each data structure is n bytes.
At first, I read it once a block (n bytes). I think it is fastest, because when I read the first block, OS should cache the whole page ( i.e. 4K) for future use. So when I fetch the next block, it doesn't need to load from hard-disk.
But when I change the reading method, each time reads 4K in to buffer, then separate from buffer, the speed boost up like 10 times.
Why? I think the reason lies in when you ask OS to load data, it will invoke system call, then Interrupt, and the OS copy the data from kernel mode to user mode for your use. This also cost time.

No comments: