Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. from __future__ import print_function, division
  2.  
  3. import timeit
  4. import msgpack
  5.  
  6.  
  7. def default(o):
  8. if isinstance(o, bytearray):
  9. return memoryview(o)
  10. return o
  11.  
  12.  
  13. runs = 1000
  14. for n in [10**i for i in range(7)]:
  15. a = bytearray('a'*n)
  16. b = bytes(a)
  17. v = memoryview(a)
  18. t_array = min(timeit.Timer(
  19. lambda: msgpack.packb(a, default=default)
  20. ).repeat(10, runs)) / runs
  21. t_bytes = min(timeit.Timer(
  22. lambda: msgpack.packb(b)
  23. ).repeat(10, runs)) / runs
  24. t_view = min(timeit.Timer(
  25. lambda: msgpack.packb(v)
  26. ).repeat(10, runs)) / runs
  27. print(n, 'bytes')
  28. print('bytes: ', t_bytes)
  29. print('bytearray: ', t_array / t_bytes * 100, '%')
  30. print('memoryview:', t_view / t_bytes * 100, '%')
  31. print('-----------------------------')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement