Is lseek() O(1) complexity?

久未见 提交于 2019-12-04 01:55:01

lseek() as implemented in ext4 will just increment the file pointer and do some validation checks. It doesn't depend on the file size, meaning it is O(1).

Also you can see this in the code, there isn't any loop nor suspicious function calls in there.

However, while this is true on ext4, it might be not true for other filesystems, as this behaviour isn't guaranteed by POSIX. But it is likely unless the filesystem is meant for a very special purpose.

lseek's complexity depends on the representation of file in your system. On most modern systems a file is organized by some clever tree-like data structure resulting into seek being executed in time O(logx(n)), where n is the size of the file and x some system depending number.

Yes, the OS already knows how to find any particular byte in the file.

No, it's not guaranteed to be O(1). The code you posted is O(1), but other filesystems' code might not be.

Yes, it will be fast enough, unless the OS or filesystem is terrible.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!