Skip to content

Instantly share code, notes, and snippets.

@ronzhin-dmitry
Created January 29, 2023 13:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ronzhin-dmitry/4d88d51ab0917c03aa60a4fa904bff41 to your computer and use it in GitHub Desktop.
Save ronzhin-dmitry/4d88d51ab0917c03aa60a4fa904bff41 to your computer and use it in GitHub Desktop.
#Worst case test of naive substring search
import time
needle = 'a'*1000 + 'b' #so we search for a string with 1000 times letter 'a' and one letter 'b' at the end
haystack = 'a'*10000 + 'b' #and the haystack is almost like this, but longer
result = []
start = time.time()
for i in range(50): #we repeat 50 times just to better track time
result = search_naive(needle, haystack)
end = time.time()
print('Positions:',result)
print('Time elapsed:',end-start)
#Example output on my machine:
#Positions: [9000]
#Time elapsed: 94.60738587379456
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment