Timing word-searching code in Python I'm using a linear search as a spell checker. I need to time my code to get averages as I'm using different searches and comparing them. However I don't believe the code I've used to time the code is accurate enough or the best method. If anyone has any suggestion that would be great.

import re
import time

start_time = time.time()
f1=open('writefile.txt', 'w+')

def linS(target, words):
    '''linear search for target in words. words need not be sorted'''
    for s in words:
        if s==target:
            return True
    return False

words = [s.strip("\n").lower() for s in open("dict.txt")] 
words.sort() # sort the list

with open("text.txt" , encoding="utf8") as text:
    content = text.read().split(" ")
    content = [item.lower() for item in content]
    content = ' '.join(content)
    content = re.findall("[a-z]+", content)

for w in content:
    if not linS(w, words):
        f1.write(w+'\n')

f1.close()

print("--- %s seconds ---" % (time.time() - start_time))
Some suggestions:


Pass the code through pycodestyle to make it more idiomatic.
Use with open() (or try/finally for a more complicated solution) to make sure files are closed whenever they are done with.
You can do .readlines() on a file pointer to avoid stripping newlines.
The dictionary is presumably controlled by you. Sort and lower-case it once, and then you don't have to do that every time the script runs.
These are logically equivalent:

    for x in y:
        if x == target:
            return True
    return False


and

    return target in y

Use descriptive names such as for word in words and `def linear_search.
Put the timing code around the lines you are trying to time. If you want to know the aggregate time of linS then get the start and end times just before and just after the last loop (which will count the time it takes to write to disk buffers) or accumulate a sum of run times within `linS`.