Match beginning of two strings
Bengt Richter
bokr at oz.net
Sun Aug 3 15:44:31 EDT 2003
On Sat, 2 Aug 2003 22:23:12 -0500, Jeff Epler <jepler at unpythonic.net> wrote:
>This is a naive implementation of the 'extract' function.
> def extract(a, b):
> m = min(len(a), len(b))
> for i in range(m):
> if a[i] != b[i]:
> return a[:i]
> return a[:m]
>
>Here's one that uses the new zip() function:
I don't see "zip" ;-)
> def extract2(a, b):
> m = min(len(a), len(b))
> for i, ai, bi in (range(m), a, b):
> if ai != bi: return a[:i]
> return a[:m]
>.. unfortunately, it seems to be slower than the first method. On my
>machine (800MHz PIII):
>$ python timeit.py -s 'import ravi' \
> 'ravi.extract("abcdefghijklmnopqrstuvwxyz","abcdefghijklmnopBHLHT")'
>10000 loops, best of 3: 32.7 usec per loop
>
My timing harness (I seem to need a new getopt for timeit.py under 2.3)
shows a slight (15-22% less time) improvement for this 2.3 alternative:
def commonprefix(s1, s2): # very little tested!
try:
for i, c in enumerate(s1):
if c != s2[i]: return s1[:i]
except IndexError:
return s1[:i]
return s1
[12:39] C:\pywk\clp>timefuns ravi -c extract -s 'abcdefghijklmnopqrstuvwxyz' -s 'abcdefghijklmno
pBHLHT' -c commonprefix -s 'abcdefghijklmnopqrstuvwxyz' -s 'abcdefghijklmnopBHLHT'
timing oh: 0.000007 ratio
extract: 0.000088 1.00
commonprefix: 0.000074 0.85
[12:39] C:\pywk\clp>timefuns ravi -c extract -s 'abcdefghijklmnopqrstuvwxyz' -s 'abcdefghijklmno
pBHLHT' -c commonprefix -s 'abcdefghijklmnopqrstuvwxyz' -s 'abcdefghijklmnopBHLHT'
timing oh: 0.000007 ratio
extract: 0.000091 1.00
commonprefix: 0.000071 0.78
[12:40] C:\pywk\clp>timefuns ravi -c extract -s 'abcdefghijklmnopqrstuvwxyz' -s 'abcdefghijklmno
pBHLHT' -c commonprefix -s 'abcdefghijklmnopqrstuvwxyz' -s 'abcdefghijklmnopBHLHT'
timing oh: 0.000007 ratio
extract: 0.000091 1.00
commonprefix: 0.000071 0.78
[12:40] C:\pywk\clp>timefuns ravi -c extract -s 'abcdefghijklmnopqrstuvwxyz' -s 'abcdefghijklmno
pBHLHT' -c commonprefix -s 'abcdefghijklmnopqrstuvwxyz' -s 'abcdefghijklmnopBHLHT'
timing oh: 0.000007 ratio
extract: 0.000088 1.00
commonprefix: 0.000071 0.81
Regards,
Bengt Richter
More information about the Python-list
mailing list