<?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>alan.lamielle.net &#187; string</title>
	<atom:link href="http://alan.lamielle.net/tags/string/feed" rel="self" type="application/rss+xml" />
	<link>http://alan.lamielle.net</link>
	<description>Alan LaMielle</description>
	<lastBuildDate>Fri, 16 Sep 2011 16:50:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Reversing Python list/string objects</title>
		<link>http://alan.lamielle.net/2009/05/04/reversing-python-liststring-objects</link>
		<comments>http://alan.lamielle.net/2009/05/04/reversing-python-liststring-objects#comments</comments>
		<pubDate>Mon, 04 May 2009 23:26:00 +0000</pubDate>
		<dc:creator>Alan LaMielle</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[list]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[reverse]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[timing]]></category>

		<guid isPermaLink="false">http://alan.lamielle.net/?p=69</guid>
		<description><![CDATA[I was interested in the relative performance of reversing a python list using the idom list[::-1] rather than the reverse method of the list class. Additionally I wanted to see if this idom was efficient for reversing strings compared to converting to a list, reversing that list, and joining the elements again using ''.join(reversed_list). Here [...]]]></description>
			<content:encoded><![CDATA[<p>I was interested in the relative performance of reversing a python list using the idom <code>list[::-1]</code> rather than the reverse method of the list class.  Additionally I wanted to see if this idom was efficient for reversing strings compared to converting to a list, reversing that list, and joining the elements again using <code>''.join(reversed_list)</code>.  Here are the results:</p>
<p>Reverse list, slicing: (0.58000000000000007, 0.64144396781921387)<br />
Reverse list, method: (0.20000000000000018, 0.19551301002502441)<br />
Reverse string, slicing: (0.45000000000000001, 0.49545693397521973)<br />
Reverse string, list: (3.71, 3.7981550693511963)</p>
<p><span id="more-69"></span>I’m using a quick Timer class that I implemented to report the running time for a chunk of code using both of <code>time.clock()</code> (the first item) and <code>time.time()</code> (the second item).  Here is the code used to produce these results:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">collections</span> <span style="color: #ff7700;font-weight:bold;">import</span> namedtuple
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">copy</span> <span style="color: #ff7700;font-weight:bold;">import</span> deepcopy
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Timer<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
   Times=namedtuple<span style="color: black;">&#40;</span><span style="color: #483d8b;">'times'</span>,<span style="color: #483d8b;">'clock time'</span><span style="color: black;">&#41;</span>
&nbsp;
   <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
      <span style="color: #008000;">self</span>.<span style="color: black;">running</span>=<span style="color: #008000;">False</span>
&nbsp;
   <span style="color: #ff7700;font-weight:bold;">def</span> _set_times<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>,field_name<span style="color: black;">&#41;</span>:
      times=Timer.<span style="color: black;">Times</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">time</span>.<span style="color: black;">clock</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>,<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: black;">&#41;</span>
      <span style="color: #008000;">setattr</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>,field_name,times<span style="color: black;">&#41;</span>
&nbsp;
   <span style="color: #ff7700;font-weight:bold;">def</span> start<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
      <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">self</span>.<span style="color: black;">running</span>: <span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">ValueError</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Timer is already running!'</span><span style="color: black;">&#41;</span>
      <span style="color: #008000;">self</span>.<span style="color: black;">running</span>=<span style="color: #008000;">True</span>
      <span style="color: #008000;">self</span>._set_times<span style="color: black;">&#40;</span><span style="color: #483d8b;">'tstart'</span><span style="color: black;">&#41;</span>
   <span style="color: #ff7700;font-weight:bold;">def</span> stop<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
      <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">self</span>.<span style="color: black;">running</span>: <span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">ValueError</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Timer is not running!'</span><span style="color: black;">&#41;</span>
      <span style="color: #008000;">self</span>.<span style="color: black;">running</span>=<span style="color: #008000;">False</span>
      <span style="color: #008000;">self</span>._set_times<span style="color: black;">&#40;</span><span style="color: #483d8b;">'tstop'</span><span style="color: black;">&#41;</span>
&nbsp;
   <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
      <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">tstop</span>.<span style="color: black;">clock</span>-<span style="color: #008000;">self</span>.<span style="color: black;">tstart</span>.<span style="color: black;">clock</span>,<span style="color: #008000;">self</span>.<span style="color: black;">tstop</span>.<span style="color: black;">time</span>-<span style="color: #008000;">self</span>.<span style="color: black;">tstart</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#41;</span>
&nbsp;
timer=Timer<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
test_string=<span style="color: #483d8b;">'Time flies like an arrow.  Fruit flies like a banana.'</span>
test_list=<span style="color: #008000;">list</span><span style="color: black;">&#40;</span>test_string<span style="color: black;">&#41;</span>
iterations=<span style="color: #ff4500;">1000000</span>
&nbsp;
timer.<span style="color: black;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span>iterations<span style="color: black;">&#41;</span>:
   new_string=test_string<span style="color: black;">&#91;</span>::-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
timer.<span style="color: black;">stop</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;">'Reverse string, slicing:'</span>,timer.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
timer.<span style="color: black;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span>iterations<span style="color: black;">&#41;</span>:
   l=<span style="color: #008000;">list</span><span style="color: black;">&#40;</span>test_string<span style="color: black;">&#41;</span>
   l.<span style="color: black;">reverse</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
   new_string=<span style="color: #483d8b;">''</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>l<span style="color: black;">&#41;</span>
timer.<span style="color: black;">stop</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;">'Reverse string, list:'</span>,timer.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
timer.<span style="color: black;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span>iterations<span style="color: black;">&#41;</span>:
   test_list=test_list<span style="color: black;">&#91;</span>::-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
timer.<span style="color: black;">stop</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;">'Reverse list, slicing:'</span>,timer.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
timer.<span style="color: black;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span>iterations<span style="color: black;">&#41;</span>:
   test_list.<span style="color: black;">reverse</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
timer.<span style="color: black;">stop</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;">'Reverse list, method:'</span>,timer.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>Feel free to use any of this code yourself, consider it under a <span class="caps">BSD</span> license.  Particularly the Timer class has been useful for me in multiple cases.</p>
<p>Based on the timing results, I’d say it looks best to use the slicing idiom on strings and the reverse method on lists.</p>
]]></content:encoded>
			<wfw:commentRss>http://alan.lamielle.net/2009/05/04/reversing-python-liststring-objects/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

