2011-10-26

Python optimizations continued ...

To give a fair treatment to pypy, I tried 2 c-like implementations similar to the cython one and the pypy 1.5 results are around 30% to 250% slower then standard python interpreter. Feel free to correct me if I did something obviously wrong here. Add those to test.py:
def htmlGB2():
    s = r
    a = array.array('c', itertools.repeat('\0', len(s)*10))
    i = 0
    for c in s:
        if (c>=u'a' and c<=u'z') or (c>=u'A' and c<=u'Z') or (c>=u'0' and c<=u'9'):
            a[i] = chr(ord(c))
            i += 1
        else:
            insert = '&#' + str(ord(c)) + ';'
            for cc in insert:
                a[i] = cc
                i += 1
    return a.tostring()

def htmlGB3():
    s = r
    result = ''
    for c in s:
        if (c>=u'a' and c<=u'z') or (c>=u'A' and c<=u'Z') or (c>=u'0' and c<=u'9'):
            result += chr(ord(c))
        else:
            result +='&#'
            result +=str(ord(c))
            result +=';'
    return result

⚫ python test.py
html7:   2.45566296577
htmlGB:   2.13124704361
htmlGB2:   11.926774025
htmlGB3:   3.90490102768

⚫ pypy-c1.5 test.py 
html7:    2.41169714928
htmlGB:   1.77979898453
htmlGB2:   15.9636659622 <-
htmlGB3:   91.441778183 <-

No comments:

Post a Comment