Re: str.count is slow

2006-02-27 Thread Ben Cartwright
[EMAIL PROTECTED] wrote: 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

Re: str.count is slow

2006-02-27 Thread Fredrik Lundh
Ben Cartwright wrote: On my machine, the output is: str: 0.29365715475 array: 0.448095498171 unicode: 0.0243757237303 This tactic typically avoids most (sometimes all) of the calls to memcmp. Other string search functions, including unicode.count, unicode.index, and

Re: str.count is slow

2006-02-27 Thread Terry Reedy
is that the C library function memcmp is slow, and str.count calls it frequently. See lines 2165+ in stringobject.c (inside function string_count): r = 0; while (i m) { if (!memcmp(s+i, sub, n)) { r++; i += n; } else { i++; } } This could be optimized as: r = 0; while (i m) { if (s