<?xml version="1.0" encoding="UTF-8"?><rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
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/"
> <channel><title>Comments on: Just for fun: binary to ascii in Python</title> <atom:link href="http://gehrcke.de/2010/01/just-for-fun-binary-to-ascii-in-python/feed/" rel="self" type="application/rss+xml" /><link>http://gehrcke.de/2010/01/just-for-fun-binary-to-ascii-in-python/</link> <description>Jan-Philip Gehrcke&#039;s website</description> <lastBuildDate>Tue, 13 Sep 2011 22:01:14 +0000</lastBuildDate> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.2.1</generator> <item><title>By: Jan-Philip Gehrcke</title><link>http://gehrcke.de/2010/01/just-for-fun-binary-to-ascii-in-python/comment-page-1/#comment-6319</link> <dc:creator>Jan-Philip Gehrcke</dc:creator> <pubDate>Tue, 13 Sep 2011 22:01:14 +0000</pubDate> <guid
isPermaLink="false">http://gehrcke.de/?p=1118#comment-6319</guid> <description>A quick benchmark to compare both methods...:
&lt;pre lang=&quot;python&quot;&gt;
#!/usr/bin/python
import random
import time
import hashlib
bitlength = 8
N = 2**21
s = N * [0]
s.extend(N * [1])
random.shuffle(s) # now s is a list of ints 0, 1 (randomly ordered)
ss = &#039;&#039;.join(map(str, s)) # a string from the same list
t1 = time.time()
rs = &#039;&#039;.join([chr(sum(bit&lt;&lt;abs(idx-bitlength)-1 for idx,bit in enumerate(y)))
for y in zip(*[s[x::bitlength] for x in range(bitlength)])])
t2 = time.time()
dt = t2-t1
print &quot;hash method 1: %s&quot; % hashlib.md5(rs).hexdigest()
print &quot;time method 1: %s&quot; % dt
t1 = time.time()
rs = &#039;&#039;.join([chr(eval(&#039;0b&#039;+ss[i:i+8])) for i in range(0, len(ss), bitlength)])
t2 = time.time()
dt = t2-t1
print &quot;hash method 2: %s&quot; % hashlib.md5(rs).hexdigest()
print &quot;time method 2: %s&quot; % dt
&lt;/pre&gt;
Result:
&lt;pre lang=&quot;text&quot;&gt;
$ ./bench.py
hash method 1: 519e71070a5eb53fa4294b39845fb6a6
time method 1: 2.73728394508
hash method 2: 519e71070a5eb53fa4294b39845fb6a6
time method 2: 4.78630614281
&lt;/pre&gt;
So your method is almost half as fast. From more tests it looks like both methods scale the same with increasing &lt;code&gt;N&lt;/code&gt;. I guess the &lt;code&gt;eval()&lt;/code&gt; method is slow.
Thanks for your comment anyway :)
JP</description> <content:encoded><![CDATA[<p>A quick benchmark to compare both methods&#8230;:</p><pre lang="python">
#!/usr/bin/python
import random
import time
import hashlib
bitlength = 8
N = 2**21
s = N * [0]
s.extend(N * [1])
random.shuffle(s) # now s is a list of ints 0, 1 (randomly ordered)
ss = ''.join(map(str, s)) # a string from the same list
t1 = time.time()
rs = ''.join([chr(sum(bit< <abs(idx-bitlength)-1 for idx,bit in enumerate(y)))
    for y in zip(*[s[x::bitlength] for x in range(bitlength)])])
t2 = time.time()
dt = t2-t1
print "hash method 1: %s" % hashlib.md5(rs).hexdigest()
print "time method 1: %s" % dt
t1 = time.time()
rs = ''.join([chr(eval('0b'+ss[i:i+8])) for i in range(0, len(ss), bitlength)])
t2 = time.time()
dt = t2-t1
print "hash method 2: %s" % hashlib.md5(rs).hexdigest()
print "time method 2: %s" % dt
</pre><p>Result: </pre><pre lang="text">
$ ./bench.py
hash method 1: 519e71070a5eb53fa4294b39845fb6a6
time method 1: 2.73728394508
hash method 2: 519e71070a5eb53fa4294b39845fb6a6
time method 2: 4.78630614281
</pre><p>So your method is almost half as fast. From more tests it looks like both methods scale the same with increasing <code>N</code>. I guess the <code>eval()</code> method is slow.</p><p>Thanks for your comment anyway <img
src='http://gehrcke.de/wp/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p><p>JP</p> ]]></content:encoded> </item> <item><title>By: dennys</title><link>http://gehrcke.de/2010/01/just-for-fun-binary-to-ascii-in-python/comment-page-1/#comment-6318</link> <dc:creator>dennys</dc:creator> <pubDate>Tue, 13 Sep 2011 21:24:54 +0000</pubDate> <guid
isPermaLink="false">http://gehrcke.de/?p=1118#comment-6318</guid> <description>for i in range(0,len(s),8): print chr(eval(&#039;0b&#039;+s[i:i+8])),</description> <content:encoded><![CDATA[<p>for i in range(0,len(s),8): print chr(eval(&#8217;0b&#8217;+s[i:i+8])),</p> ]]></content:encoded> </item> </channel> </rss>
<!-- Dynamic page generated in 1.644 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2011-09-16 22:03:18 -->

