<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Generator" CONTENT="MS Exchange Server version 5.5.2657.73">
<TITLE>RE: [Python-Dev] string find(substring) vs. substring in string</TITLE>
</HEAD>
<BODY>

<P><FONT SIZE=2>[Fredrik Lundh]</FONT>
</P>

<P><FONT SIZE=2>#- any special reason why &quot;in&quot; is faster if the substring is found, but</FONT>
<BR><FONT SIZE=2>#- a lot slower if it's not in there?</FONT>
</P>

<P><FONT SIZE=2>Maybe because it stops searching when it finds it?</FONT>
</P>

<P><FONT SIZE=2>The time seems to be very dependant of the position of the first match:</FONT>
</P>

<P><FONT SIZE=2>&nbsp; fbatista@pytonisa ~/ota&gt; python /usr/local/lib/python2.3/timeit.py -s &quot;s = 'not there'*100&quot; &quot;'not there' in s&quot;</FONT>
<BR><FONT SIZE=2>&nbsp; 1000000 loops, best of 3: 0.222 usec per loop</FONT>
</P>

<P><FONT SIZE=2>&nbsp; fbatista@pytonisa ~/ota&gt; python /usr/local/lib/python2.3/timeit.py -s &quot;s = 'blah blah'*20 + 'not there'*100&quot; &quot;'not there' in s&quot;</FONT></P>

<P><FONT SIZE=2>&nbsp; 100000 loops, best of 3: 5.54 usec per loop</FONT>
</P>

<P><FONT SIZE=2>&nbsp; fbatista@pytonisa ~/ota&gt; python /usr/local/lib/python2.3/timeit.py -s &quot;s = 'blah blah'*40 + 'not there'*100&quot; &quot;'not there' in s&quot;</FONT></P>

<P><FONT SIZE=2>&nbsp; 100000 loops, best of 3: 10.8 usec per loop</FONT>
</P>
<BR>

<P><FONT SIZE=2>.&nbsp;&nbsp;&nbsp; Facundo</FONT>
</P>

<P><FONT SIZE=2>Bitácora De Vuelo: <A HREF="http://www.taniquetil.com.ar/plog" TARGET="_blank">http://www.taniquetil.com.ar/plog</A></FONT>
<BR><FONT SIZE=2>PyAr - Python Argentina: <A HREF="http://pyar.decode.com.ar/" TARGET="_blank">http://pyar.decode.com.ar/</A></FONT>
</P>
<BR>
<BR>

</BODY>
</HTML>