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

前端 未结 3 1331
死守一世寂寞
死守一世寂寞 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:22

    I have seen similar situations while dealing with arduinos interfacing with EEPROM. Basically, in order to write or read from a chip or data structure, you have to send a write/read enable command, send a starting location, and then grab the first character. If you grab multiple bytes, however, most chips will auto-increment their target address registers. Thus, there is some overhead for starting a read/write operation. It's the difference between:

    • Start communications
    • Send read enable
    • Send read command
    • Send address 1
    • Grab data from target 1
    • End communications
    • Start communications
    • Send read enable
    • Send read command
    • Send address 2
    • Grab data from target 2
    • End communications

    and

    • Start communications
    • Send read enable
    • Send read command
    • Send address 1
    • Grab data from target 1
    • Grab data from target 2
    • End communications

    Just, in terms of machine instructions, reading multiple bits/bytes at a time clears a lot of overhead. It's even worse when some chips require you to idle for a few clock cycles after the read/write enable is send to let a mechanical process physically move a transistor into place to enable the reading or writing.

提交回复
热议问题