str.count is slow

chrisperkins99 at gmail.com chrisperkins99 at gmail.com
Mon Feb 27 15:54:56 EST 2006


It seems to me that str.count is awfully slow.  Is there some reason
for this?
Evidence:

######## str.count time test ########
import string
import time
import array

s = string.printable * int(1e5) # 10**7 character string
a = array.array('c', s)
u = unicode(s)
RIGHT_ANSWER = s.count('a')

def main():
    print 'str:    ', time_call(s.count, 'a')
    print 'array:  ', time_call(a.count, 'a')
    print 'unicode:', time_call(u.count, 'a')

def time_call(f, *a):
    start = time.clock()
    assert RIGHT_ANSWER == f(*a)
    return time.clock()-start

if __name__ == '__main__':
    main()

###### end ########

On my machine, the output is:

str:     0.29365715475
array:   0.448095498171
unicode: 0.0243757237303

If a unicode object can count characters so fast, why should an str
object be ten times slower?  Just curious, really - it's still fast
enough for me (so far).

This is with Python 2.4.1 on WinXP.


Chris Perkins




More information about the Python-list mailing list