Skip to content

Instantly share code, notes, and snippets.

@ronzhin-dmitry
Created January 30, 2023 13:24
Show Gist options
  • Save ronzhin-dmitry/15f933c58c8157a20583053fc45d00e1 to your computer and use it in GitHub Desktop.
Save ronzhin-dmitry/15f933c58c8157a20583053fc45d00e1 to your computer and use it in GitHub Desktop.
#Just the same worst-case test as for naive case. But now we will have a much better time complexity
import time
needle = 'a'*1000 + 'b'
haystack = 'a'*10000 + 'b'
result = []
start = time.time()
buf_memory = [0] * (len(needle) + len(haystack) + 1)
start2 = time.time()
for i in range(50):
result = search_z_func(needle, haystack, buf_memory)
end = time.time()
print('Positions:',result)
print('Time elapsed (with mem-alloc):',end-start)
print('Time elapsed (search only):',end-start2)
#Example output:
#Positions: [9000]
#Time elapsed (with mem-alloc): 1.3396055698394775
#Time elapsed (search only): 1.3396055698394775
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment