Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. class SimpleEditor:
  2. def __init__(self, document):
  3. self.document = document
  4. self.dictionary = set()
  5. with open("/usr/share/dict/words") as input_dictionary:
  6. for line in input_dictionary:
  7. words = line.strip().split(" ")
  8. for word in words:
  9. self.dictionary.add(word)
  10. self.paste_text = ""
  11.  
  12.  
  13. def cut(self, i, j):
  14. self.paste_text = self.document[i:j]
  15. self.document = self.document[:i] + self.document[j:]
  16.  
  17. def copy(self, i, j):
  18. self.paste_text = self.document[i:j]
  19.  
  20. def paste(self, i):
  21. self.document = self.document[:i] + self.paste_text + self.document[i:]
  22.  
  23. def get_text(self):
  24. return self.document
  25.  
  26. def misspellings(self):
  27. result = 0
  28. for word in self.document.split(" "):
  29. if word not in self.dictionary:
  30. result = result + 1
  31. return result
  32.  
  33. import timeit
  34.  
  35. class EditorBenchmarker:
  36. new_editor_case = """
  37. from __main__ import SimpleEditor
  38. s = SimpleEditor("{}")"""
  39.  
  40. editor_cut_paste = """
  41. for n in range({}):
  42. if n%2 == 0:
  43. s.cut(1, 3)
  44. else:
  45. s.paste(2)"""
  46.  
  47. editor_copy_paste = """
  48. for n in range({}):
  49. if n%2 == 0:
  50. s.copy(1, 3)
  51. else:
  52. s.paste(2)"""
  53.  
  54. editor_get_text = """
  55. for n in range({}):
  56. s.get_text()"""
  57.  
  58. editor_mispellings = """
  59. for n in range({}):
  60. s.misspellings()"""
  61.  
  62. def __init__(self, cases, N):
  63. self.cases = cases
  64. self.N = N
  65. self.editor_cut_paste = self.editor_cut_paste.format(N)
  66. self.editor_copy_paste = self.editor_copy_paste.format(N)
  67. self.editor_get_text = self.editor_get_text.format(N)
  68. self.editor_mispellings = self.editor_mispellings.format(N)
  69.  
  70. def benchmark(self):
  71. for case in self.cases:
  72. print("Evaluating case: {}".format(case))
  73. new_editor = self.new_editor_case.format(case)
  74. cut_paste_time = timeit.timeit(stmt=self.editor_cut_paste,setup=new_editor,number=1)
  75. print("{} cut paste operations took {} s".format(self.N, cut_paste_time))
  76. copy_paste_time = timeit.timeit(stmt=self.editor_copy_paste,setup=new_editor,number=1)
  77. print("{} copy paste operations took {} s".format(self.N, copy_paste_time))
  78. get_text_time = timeit.timeit(stmt=self.editor_get_text,setup=new_editor,number=1)
  79. print("{} text retrieval operations took {} s".format(self.N, get_text_time))
  80. mispellings_time = timeit.timeit(stmt=self.editor_mispellings,setup=new_editor,number=1)
  81. print("{} mispelling operations took {} s".format(self.N, mispellings_time))
  82.  
  83.  
  84. if __name__ == "__main__":
  85. b = EditorBenchmarker(["hello friends"], 100)
  86. b.benchmark()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement