Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import print_function, division
- import timeit
- import msgpack
- def default(o):
- if isinstance(o, bytearray):
- return memoryview(o)
- return o
- runs = 1000
- for n in [10**i for i in range(7)]:
- a = bytearray('a'*n)
- b = bytes(a)
- v = memoryview(a)
- t_array = min(timeit.Timer(
- lambda: msgpack.packb(a, default=default)
- ).repeat(10, runs)) / runs
- t_bytes = min(timeit.Timer(
- lambda: msgpack.packb(b)
- ).repeat(10, runs)) / runs
- t_view = min(timeit.Timer(
- lambda: msgpack.packb(v)
- ).repeat(10, runs)) / runs
- print(n, 'bytes')
- print('bytes: ', t_bytes)
- print('bytearray: ', t_array / t_bytes * 100, '%')
- print('memoryview:', t_view / t_bytes * 100, '%')
- print('-----------------------------')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement