<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Patrick&#039;s playground &#187; method speed</title>
	<atom:link href="http://www.vankouteren.eu/blog/tag/method-speed/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.vankouteren.eu/blog</link>
	<description>Random thoughts, problems and solutions</description>
	<lastBuildDate>Sun, 29 Jan 2012 07:53:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Python: check for substring speed</title>
		<link>http://www.vankouteren.eu/blog/2009/06/python-check-for-substring-speed/</link>
		<comments>http://www.vankouteren.eu/blog/2009/06/python-check-for-substring-speed/#comments</comments>
		<pubDate>Mon, 29 Jun 2009 15:07:23 +0000</pubDate>
		<dc:creator>Patrick van Kouteren</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[comparison]]></category>
		<category><![CDATA[method speed]]></category>
		<category><![CDATA[substring]]></category>

		<guid isPermaLink="false">http://www.vankouteren.eu/blog/?p=110</guid>
		<description><![CDATA[I was looking for options on how to check if a certain substring (in my case ' FROM ') is present in a SQL query string when I found this blog entry. Just for fun I decided to have a look at how fast these checks would be compared to each other. I was dealing [...]]]></description>
			<content:encoded><![CDATA[<p>I was looking for options on how to check if a certain substring (in my case ' FROM ') is present in a SQL query string when I found <a title="Python check for substring" href="http://bka-bonn.de/wordpress/index.php/2008/12/26/python-trick-check-for-substring/" target="_blank">this</a> blog entry. Just for fun I decided to have a look at how fast these checks would be compared to each other.</p>
<p><span id="more-110"></span></p>
<p>I was dealing with a two queries, knowing:</p>
<pre class="sql"><span style="color: #993333; font-weight: bold;">SELECT</span> oid,typname,typlen,typlem,typdefault,typbasetype,typnotnull,typtype
<span style="color: #993333; font-weight: bold;">FROM</span> pg_type;</pre>
<p>And</p>
<pre class="sql"><span style="color: #993333; font-weight: bold;">SELECT</span> attname,attnum,atttypid,attndims,attnotnull,atthasdef,
pg_get_expr<span style="color: #66cc66;">&#40;</span>adbin,adrelid<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> adbin
<span style="color: #993333; font-weight: bold;">FROM</span> pg_attribute <span style="color: #993333; font-weight: bold;">LEFT</span> <span style="color: #993333; font-weight: bold;">JOIN</span> pg_attrdef <span style="color: #993333; font-weight: bold;">ON</span> attrelid = adrelid <span style="color: #993333; font-weight: bold;">AND</span> attnum = adnum
<span style="color: #993333; font-weight: bold;">WHERE</span> attisdropped = false <span style="color: #993333; font-weight: bold;">AND</span> attnum &amp;gt; <span style="color: #cc66cc;">0</span> <span style="color: #993333; font-weight: bold;">AND</span> attrelid <span style="color: #993333; font-weight: bold;">IN</span>
<span style="color: #66cc66;">&#40;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> oid <span style="color: #993333; font-weight: bold;">FROM</span> pg_class <span style="color: #993333; font-weight: bold;">WHERE</span> relname=%s <span style="color: #993333; font-weight: bold;">AND</span> relkind=<span style="color: #ff0000;">'r'</span> <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> attnum<span style="color: #66cc66;">&#41;</span>;</pre>
<p>The code is pretty simple:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
t1 = <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
      <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #483d8b;">' FROM '</span> <span style="color: #ff7700;font-weight:bold;">in</span> query:
          <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'IN found it!'</span>
      t2 = <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
      <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Took me &quot;</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>t2-t1<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot; sec.&quot;</span>
&nbsp;
      t3 = <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
      <span style="color: #ff7700;font-weight:bold;">if</span> query.<span style="color: black;">find</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">' FROM '</span><span style="color: black;">&#41;</span> != <span style="color: #ff4500;">-1</span>:
          <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'FIND found it!'</span>
      t4 = <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
      <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Took me &quot;</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>t4-t3<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot; sec.&quot;</span></pre>
<p>The results look as follows:</p>
<pre>IN found it!
Took me 4.72068786621e-05 sec.
FIND found it!
Took me 1.09672546387e-05 sec.
IN found it!
Took me 4.19616699219e-05 sec.
FIND found it!
Took me 1.12056732178e-05 sec.
IN found it!
Took me 3.48091125488e-05 sec.
FIND found it!
Took me 9.05990600586e-06 sec.
Took me 1.90734863281e-06 sec.
Took me 5.00679016113e-06 sec.
IN found it!
Took me 2.59876251221e-05 sec.
FIND found it!
Took me 1.19209289551e-05 sec.
Took me 9.53674316406e-07 sec.
Took me 1.90734863281e-06 sec.
IN found it!
Took me 0.00103211402893 sec.
FIND found it!
Took me 2.50339508057e-05 sec.
Took me 9.53674316406e-07 sec.
Took me 4.05311584473e-06 sec.</pre>
<p>As we can see: if we use the if-in test, we get only one result even if there are more instances of ' FROM ' in the string. When using the find method, all instances are retrieved. When having only one instance in your string, the find method is usually faster. When having multiple instances, the if-in test will be faster.<br />
It doesn't make much sense with small strings, but if you're just interested in finding a substring one or more times in a large string or a piece of text, it can make a difference.<br />
So far my little experiment. Knowing the answer, I can sleep well again tonight <img src='http://www.vankouteren.eu/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.vankouteren.eu/blog/2009/06/python-check-for-substring-speed/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

