Guest User

Scramble function comparison

a guest
Jul 30th, 2017
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.43 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Sun Jul 26 09:14:28 2017
  5.  
  6. @author: martin
  7. """
  8. from random import random
  9. from random import choice
  10. from string import ascii_lowercase
  11. import timeit
  12. import matplotlib.pyplot as plt
  13. from collections import OrderedDict
  14. from strgen import StringGenerator as SG
  15.  
  16. # Compared functions
  17. def scramble(s1, s2):
  18.     return not any(s1.count(char) < s2.count(char) for char in set(s2))
  19.  
  20. def scramblef(s1, s2):
  21.     h = [0] * 26
  22.     for c in s1:
  23.         h[ord(c) - 97] += 1
  24.     for c in s2:
  25.         h[ord(c) - 97] -= 1
  26.     return 0 == sum(n < 0 for n in h)
  27.  
  28.  
  29. def string_pair_generator(maximum = 1000):
  30.     """
  31.    Some typical use-case, generates a pair of strings,
  32.    the first string 1/10 the length of the second. Last element of the returned
  33.    tuple is the length of the second string.
  34.    """
  35.     for i in range(1, maximum, 100):
  36.         print("Progress {} / {}, t = {}".format(i, maximum, timeit.default_timer()))
  37.         for j in range(10, maximum // 10, 10):
  38.             s1 = SG("[a-z]{{{}}}".format(i))
  39.             s2 = SG("[a-z]{{{}}}".format(j))
  40.             for k in range(1000): # more result of the same length
  41.                 yield (s1.render(), s2.render(), i) # string pair
  42.        
  43.        
  44. def performance_test(test_input, UUT1, UUT2):
  45.     results = OrderedDict() # ordered dict of tuples length -> ( t1, t2)
  46.     for pair in test_input:
  47.         t1 = timeit.default_timer()
  48.         UUT1(pair[0], pair[1])
  49.         dt1 = timeit.default_timer() - t1
  50.         t2 = timeit.default_timer()
  51.         UUT2(pair[0], pair[1])
  52.         dt2 = timeit.default_timer() - t2
  53.  
  54.         # Add to results
  55.         tmp = results.get(pair[2], (0, 0))
  56.         results[pair[2]] =  (tmp[0] + dt1, tmp[1] + dt2)
  57.     return results
  58.  
  59. tst_time = timeit.default_timer()
  60.      
  61. test_results = performance_test(string_pair_generator(), scramble, scramblef)
  62. print("Testing took {}s.".format(timeit.default_timer() - tst_time))
  63.  
  64.  
  65. times1 = list()
  66. times2 = list()
  67. lengths = list()
  68. for key, val in test_results.items():
  69.     times1.append(val[0])
  70.     times2.append(val[1])
  71.     lengths.append(key)
  72.  
  73.  
  74. plt.plot(lengths, times1)
  75. plt.plot(lengths, times2)
  76.  
  77. plt.legend(['scramble', 'scramblef'], loc = 'upper left')
  78.  
  79. plt.xlabel('Input length [1]')
  80. plt.ylabel('Time [s]')
  81. plt.title('Time of computation dependend on input length')
  82. plt.grid(True)
  83. plt.savefig("scramble_test.png")
  84. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment