2011-10-27

Python optimizations, third round up !

Thanks to @fijall (a pypy core developer), we gained 2 new implementations !
def htmlGB4():
    l = []
    for c in r:
        if (c>=u'a' and c<=u'z') or (c>=u'A' and c<=u'Z') or (c>=u'0' and c<=u'9'):
            l.append(chr(ord(c)))
        else:
            insert = '&#' + str(ord(c)) + ';'
            l.append(insert)
    return ''.join(l)
assert(html7() == htmlGB4())

from cStringIO import StringIO
 
def htmlGB5():
    s = StringIO()
    for c in r:
        if (c>=u'a' and c<=u'z') or (c>=u'A' and c<=u'Z') or (c>=u'0' and c<=u'9'):
            s.write(chr(ord(c)))
        else:
            insert = '&#' + str(ord(c)) + ';'
            s.write(insert)
    return s.getvalue()
The specialized pypy StringBuilder he passed on looks interesting but for some reason it didn't appear on the build I had on gentoo on PyPy 1.5 I ported the htmlGB5 to cython also. When no data appears it means I didn't ported it. Here is the new comparative graph, I omitted htmlGB3 because it was throwing the graph out of scale.
So pypy shines with the StringIO where it is more then twice as fast as cython where cython is more then twice as fast as python. Thank a lot for the contribution. Enjoy !