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

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

    Reading from a file handle byte-for-byte will be generally slower than reading chunked.

    In general, every read() call corresponds to a C read() call in Python. The total result involves a system call requesting the next char. For a file of 2 kb, this means 2000 calls to the kernel; each requiring a function call, request to the kernel, then awaiting response, passing that through the return.

    Most notable here is awaiting response, the system call will block until your call is acknowledged in a queue, so you have to wait.

    Fewer calls the better, so more bytes is faster; which is why buffered io is in fairly common use.

    In python, buffering can be provided by io.BufferedReader or through the buffering keyword argument on open for files

提交回复
热议问题