| 1 |
from msgpack import fallback |
| 2 |
|
| 3 |
try: |
| 4 |
from msgpack import _cmsgpack |
| 5 |
|
| 6 |
has_ext = True |
| 7 |
except ImportError: |
| 8 |
has_ext = False |
| 9 |
import timeit |
| 10 |
|
| 11 |
|
| 12 |
def profile(name, func): |
| 13 |
times = timeit.repeat(func, number=1000, repeat=4) |
| 14 |
times = ", ".join(["%8f" % t for t in times]) |
| 15 |
print("%-30s %40s" % (name, times)) |
| 16 |
|
| 17 |
|
| 18 |
def simple(name, data): |
| 19 |
if has_ext: |
| 20 |
packer = _cmsgpack.Packer() |
| 21 |
profile("packing %s (ext)" % name, lambda: packer.pack(data)) |
| 22 |
packer = fallback.Packer() |
| 23 |
profile("packing %s (fallback)" % name, lambda: packer.pack(data)) |
| 24 |
|
| 25 |
data = packer.pack(data) |
| 26 |
if has_ext: |
| 27 |
profile("unpacking %s (ext)" % name, lambda: _cmsgpack.unpackb(data)) |
| 28 |
profile("unpacking %s (fallback)" % name, lambda: fallback.unpackb(data)) |
| 29 |
|
| 30 |
|
| 31 |
def main(): |
| 32 |
simple("integers", [7] * 10000) |
| 33 |
simple("bytes", [b"x" * n for n in range(100)] * 10) |
| 34 |
simple("lists", [[]] * 10000) |
| 35 |
simple("dicts", [{}] * 10000) |
| 36 |
|
| 37 |
|
| 38 |
main() |