Python efficient way to check if very large string contains a substring

后端 未结 8 1846
無奈伤痛
無奈伤痛 2021-01-02 05:04

Python is not my best language, and so I\'m not all that good at finding the most efficient solutions to some of my problems. I have a very large string (coming from a 30 MB

8条回答
  •  我在风中等你
    2021-01-02 05:57

    Is it really slow? You're talking about 30MB string; let's try it with even bigger string:

    In [12]: string="agu82934u"*50*1024*1024+"string to be found"
    
    In [13]: len(string)
    Out[13]: 471859218
    
    In [14]: %timeit "string to be found" in string
    1 loops, best of 3: 335 ms per loop
    
    In [15]: %timeit "string not to be found" in string
    1 loops, best of 3: 200 ms per loop
    

    I wouldn't say that 335 ms is much time looking for substring in 450MB string.

提交回复
热议问题