3
\$\begingroup\$

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))
\$\endgroup\$
  • 2
    \$\begingroup\$ Did you consider using the timeit module? \$\endgroup\$ – Gareth Rees Apr 10 '18 at 21:35
  • \$\begingroup\$ @GarethRees I looked into it but couldnt really grasp it \$\endgroup\$ – J Doe Apr 10 '18 at 21:36
  • \$\begingroup\$ Why do you not consider this to be accurate? \$\endgroup\$ – l0b0 Apr 10 '18 at 21:45
  • \$\begingroup\$ @l0b0 I assumed it would time how long it takes to write to the file as well as the time to carry out the searches. Would this not make a difference? \$\endgroup\$ – J Doe Apr 10 '18 at 21:59
  • \$\begingroup\$ It times whatever you have wrapped it around, yes. But what are you trying to time? findall? linS? Defining the problem is going to be very useful. \$\endgroup\$ – l0b0 Apr 10 '18 at 22:08
2
\$\begingroup\$

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`.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.