Complexity of f.seek() in Python

前端 未结 2 1981
梦谈多话
梦谈多话 2021-01-17 18:37

Does f.seek(500000,0) go through all the first 499999 characters of the file before getting to the 500000th? In other words, is f.seek(n,0) of orde

2条回答
  •  半阙折子戏
    2021-01-17 19:02

    It would depend on the implementation of f. However, in normal file-system files, it is O(1).

    If python implements f on text files, it could be implemented as O(n), as each character may need to be inspected to manage cr/lf pairs correctly.

    • This would be based on whether f.seek(n,0) gave the same result as a loop of reading chars, and (depending on OS) cr/lf were shrunk to lf or lf expanded to cr/lf

    If python implements f on a compressed stream, then the order would b O(n), as decompression may require some working of blocks, and decompression.

提交回复
热议问题