Last active
December 27, 2016 03:25
-
-
Save patwooky/a2f8a21aab69640fad6dda2cfe5a1d35 to your computer and use it in GitHub Desktop.
Comparing strings methods for testing equality. http://stackoverflow.com/questions/18437798/find-vs-in-operation-in-string-python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import timeit | |
from functools import partial | |
def myEquals(numItems): | |
xList = [str(x) for x in xrange(numItems)] | |
# print xList | |
xCompList = [] | |
for n in xrange(len(xList)): | |
for o in xrange(n): | |
for p in xrange(len(xList)): | |
# print n | |
if xList[n][:o] == xList[p][:o]: | |
xCompList.append(True) | |
else: | |
xCompList.append(False) | |
def mySubstring(numItems): | |
xList = [str(x) for x in xrange(numItems)] | |
# print xList | |
xCompList = [] | |
for n in xrange(len(xList)): | |
for o in xrange(n): | |
for p in xrange(len(xList)): | |
# print n | |
if xList[n][:o] in xList[p]: | |
xCompList.append(True) | |
else: | |
xCompList.append(False) | |
for x in [10, 50, 100, 200, 300]: | |
print 'x is {} items'.format(x) | |
myEqualsTime = timeit.timeit(partial(myEquals, x), number=1) | |
mySubstringTime = timeit.timeit(partial(mySubstring, x), number=1) | |
print 'myEqualsTime\t{}\nmySubstringTime {}\n'.format(myEqualsTime, mySubstringTime) | |
# x is 10 items | |
# myEqualsTime 0.000173224523302 | |
# mySubstringTime 0.000122451818196 | |
# x is 50 items | |
# myEqualsTime 0.0169367504359 | |
# mySubstringTime 0.0126522167832 | |
# x is 100 items | |
# myEqualsTime 0.136645989234 | |
# mySubstringTime 0.113794005322 | |
# x is 200 items | |
# myEqualsTime 0.961331251795 | |
# mySubstringTime 0.784282135816 | |
# x is 300 items | |
# myEqualsTime 3.14249039692 | |
# mySubstringTime 2.74275902293 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment