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))