<div dir="auto"><div class="gmail_extra" dir="auto"><div class="gmail_quote">On Feb 8, 2018 13:06, "Serhiy Storchaka" <<a href="mailto:storchaka@gmail.com" target="_blank">storchaka@gmail.com</a>> wrote:<br type="attribution"><blockquote class="m_-5283829316576763253m_-3912614005060766651quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">08.02.18 12:45, Franklin? Lee пише:<div class="m_-5283829316576763253m_-3912614005060766651quoted-text"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Could it be that re uses an optimization that can also be used in str? CPython uses a modified Boyer-Moore for str.find:<br>
<a href="https://github.com/python/cpython/blob/master/Objects/stringlib/fastsearch.h" rel="noreferrer" target="_blank">https://github.com/python/cpyt<wbr>hon/blob/master/Objects/string<wbr>lib/fastsearch.h</a><br>
<a href="http://effbot.org/zone/stringlib.htm" rel="noreferrer" target="_blank">http://effbot.org/zone/stringl<wbr>ib.htm</a><br>
Maybe there's a minimum length after which it's better to precompute a table.<br>
</blockquote>
<br></div>
Yes, there is a special optimization in re here. It isn't free, you need to spend some time for preparing it. You need a special object that keeps an optimized representation for faster search. This makes it very unlikely be used in str, because you need either spend the time for compilation on every search, or use some kind of caching, which is not free too, adds complexity and increases memory consumption. Note also in case of re the compiler is implemented in Python. This reduces the complexity.<br></blockquote></div></div><div dir="auto"><br></div><div dir="auto">The performance of the one-needle case isn't really relevant, though, is it? This idea is for the multi-needle case, and my tests showed that re performs even worse than a loop of `.find`s. How do re and .find scale with both number and lengths of needles on your machine?</div><div class="gmail_extra" dir="auto"><div class="gmail_quote"><blockquote class="m_-5283829316576763253m_-3912614005060766651quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"></blockquote></div></div></div>