|
From: Noel O'B. <bao...@gm...> - 2007-05-07 08:45:27
|
> A simple little test shows that find() is in fact the worst, but "word in
> line" is at least comparable to "line[i:j] == word":
> >>> import timeit
> >>> t1 = timeit.Timer("'a' in 'abcdefg'")
> >>> t2 = timeit.Timer("'abcdefg'[:1] == 'a'")
> >>> t3 = timeit.Timer("'abcdefg'.find('a')")
> >>> min(t1.repeat(repeat=100, number=1000000))
> 0.18727612495422363
> >>> min(t2.repeat(repeat=100, number=1000000))
> 0.3044281005859375
> >>> min(t3.repeat(repeat=100, number=1000000))
> 0.7338860034942627
I was surprised by this, but it's the same for me. However, my earlier
experiments showed 'in' to be the worst, and that is because most
lines don't match the expression. I will show some timings for a large
log file when I get a chance.
Noel
|