Why is reading one byte 20x slower than reading 2, 3, 4, … bytes from a file?

前端 未结 3 1222
死守一世寂寞
死守一世寂寞 2021-02-05 01:54

I have been trying to understand the tradeoff between read and seek. For small \"jumps\" reading unneeded data is faster than skipping it with se

3条回答
  •  感情败类
    2021-02-05 02:40

    I was able to reproduce the issue with your code. However, I noticed the following: can you verify that the issue disappears if you replace

    file.seek(randint(0, file.raw._blksize), 1)
    

    with

    file.seek(randint(0, file.raw._blksize), 0)
    

    in setup? I think you might just run out of data at some point during reading 1 byte. Reading 2 bytes, 3 bytes and so on won't have any data to read, so that's why it's much faster.

提交回复
热议问题