Guest User

Untitled

a guest
Dec 24th, 2023
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.22 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import timeit
  4.  
  5. setup = "import numpy as np; idxlen = {}; rng = np.random.default_rng(); matrix = -np.ones([100,idxlen],dtype=int)"
  6.  
  7. test1 = "rand_idxs = np.arange(idxlen); rng.shuffle(rand_idxs); matrix[0][:] = rand_idxs"
  8. test2 = "matrix[0][:] = rng.choice(idxlen, idxlen, replace=False)"
  9. test3 = "matrix[0][:] = rng.permutation(idxlen)"
  10. idx_lengths = np.logspace(1, 5, num=10, dtype=int)
  11. results1 = []
  12. results2 = []
  13. results3 = []
  14.  
  15. for idxlen in idx_lengths:
  16.     time1 = timeit.timeit(test1.format(idxlen), setup.format(idxlen), number=10000)
  17.     time2 = timeit.timeit(test2.format(idxlen), setup.format(idxlen), number=10000)
  18.     time3 = timeit.timeit(test3.format(idxlen), setup.format(idxlen), number=10000)
  19.     results1.append(time1)
  20.     results2.append(time2)
  21.     results3.append(time3)
  22.  
  23. plt.figure(figsize=(10, 6))
  24. plt.plot(idx_lengths, results1, label='rng.shuffle')
  25. plt.plot(idx_lengths, results2, label='rng.choice')
  26. plt.plot(idx_lengths, results3, label='rng.permutation')
  27. plt.xscale('log')
  28. plt.yscale('linear')
  29.  
  30. plt.title('Shuffle Timings for Different Methods')
  31. plt.xlabel('Index Length (idxlen)')
  32. plt.ylabel('Time (seconds)')
  33. plt.legend()
  34. plt.show()
  35.  
Advertisement
Add Comment
Please, Sign In to add comment