Python is not my best language, and so I\'m not all that good at finding the most efficient solutions to some of my problems. I have a very large string (coming from a 30 MB
I do not see how to make it more optimal on the comparison, to be honest. But you can use less memory and lose less time with I/O if you read the file line by line:
has_small_string = False
for line in large_file:
if small_string in line:
has_small_string = True
break
if has_small_string:
# ... Your stuff here ...
This is no revolutionary improvement, and can be even less useful if you really need the large string in the memory, but it may be helpful, so I am posting here :)
If you just want to check if that substring exists,
for line in open("file"):
if substring in line:
print "exists"
sys.exit() # or break to do some other stuff