Random disk writes
I need to perform random updates in-place to a file. Let's I need to update a file at offset k1, k2, ...., kn. From a performance perspective does it matter if I write in any order or does the performance improve if I write in increasing order of offsets? More specifically I am going to buffer of bunch of updates in RAM and then update. So when I ready to flush the updates to disk I know the offset I need to writ开发者_如何学Ce to.
Moving the head of a hard drive from one cylinder to another is the largest performance killer when dealing with large files on rotating disks. The further the head has to move, the larger the impact.
Do the writes in order. That will cause (statistically speaking at least) all sectors on a cylinder to be written together (no head move), then the head to move to an adjacent cylinder (shortest possible head move).
Note that if you are dealing with a logical disk that has multiple backing physical disks (e.g. RAID, NAS) the head seek issue is somewhat mitigated by having more disks, but unless you have specific knowledge of the mapping from logical sector to physical storage, doing the updates in sector order is still most likely to minimize head moves.
Unless you know how your offsets translate into specific cylinders/heads/sectors, I wouldn't worry about it. Your disk controller may re-order requests for efficiency on its own, and if your disk gets fragmented you don't know whether the file's logical blocks are sequential or not anyway.
精彩评论