Quicker to os.walk or glob?

后端 未结 4 679
情深已故
情深已故 2020-12-23 10:06

I\'m messing around with file lookups in python on a large hard disk. I\'ve been looking at os.walk and glob. I usually use os.walk as I find it much neater and seems to b

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-23 10:29

    Don't waste your time for optimization before measuring/profiling. Focus on making your code simple and easy to maintain.

    For example, in your code you precompile RE, which does not give you any speed boost, because re module has internal re._cache of precompiled REs.

    1. Keep it simple
    2. if it's slow, then profile
    3. once you know exactly what needs to be optimized do some tweaks and always document it

    Note, that some optimization done several years prior can make code run slower compared to "non-optimized" code. This applies especially for modern JIT based languages.

提交回复
热议问题