Guest User

Untitled

a guest
Jan 30th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import string, random, time
  4.  
  5. def randString(size=10, chars=string.ascii_uppercase):
  6. return ''.join(random.choice(chars) for _ in range(size))
  7.  
  8. def is_substring(a,b):
  9. found = False
  10. for x in range(len(b)):
  11. for i in range(len(a)):
  12. if x+i >= len(b):
  13. found = False
  14. break
  15. if a[i] == b[x+i]:
  16. found = True
  17. else:
  18. found = False
  19. break
  20. if found:
  21. break
  22. return found
  23.  
  24.  
  25. def default_is_substring(a,b):
  26. return a in b
  27.  
  28. a = "CAT"
  29.  
  30. xvals = np.arange(0,100,10)
  31.  
  32. mine = []
  33. theirs = []
  34.  
  35. for x in xvals:
  36. b = randString(x)
  37.  
  38. starttime = time.time()
  39. is_substring(a,b)
  40. endtime = time.time()
  41. mine.append(endtime - starttime)
  42.  
  43. starttime = time.time()
  44. default_is_substring(a,b)
  45. endtime = time.time()
  46. theirs.append(endtime - starttime)
  47.  
  48.  
  49. plt.plot(xvals,mine)
  50.  
  51. plt.plot(xvals,theirs)
  52.  
  53.  
  54. plt.grid()
  55. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment