objpickles.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # Benchmark for pickling carray/ctable objects
  2. from __future__ import print_function
  3. import os.path
  4. import shutil
  5. import contextlib
  6. import time
  7. import pickle
  8. import numpy as np
  9. import numpy.testing
  10. import bcolz
  11. @contextlib.contextmanager
  12. def ctime(message=None):
  13. "Counts the time spent in some context"
  14. t = time.time()
  15. yield
  16. if message:
  17. print(message + ":\t", end="")
  18. print(round(time.time() - t, 4), "sec")
  19. carootdir = "carraypickle.bcolz"
  20. if os.path.exists(carootdir):
  21. shutil.rmtree(carootdir)
  22. ctrootdir = "ctablepickle.bcolz"
  23. if os.path.exists(ctrootdir):
  24. shutil.rmtree(ctrootdir)
  25. N = int(1e7)
  26. a = bcolz.arange(N, dtype="int32")
  27. b = bcolz.arange(N, dtype="float32")
  28. ca = bcolz.carray(a, rootdir=carootdir)
  29. ct = bcolz.ctable([ca, b], names=['a', 'b'], rootdir=ctrootdir)
  30. with ctime("Time spent pickling carray with N=%d" % N):
  31. s = pickle.dumps(ca)
  32. with ctime("Time spent unpickling carray with N=%d" % N):
  33. ca2 = pickle.loads(s)
  34. np.testing.assert_allclose(ca2[:], a)
  35. with ctime("Time spent pickling ctable with N=%d" % N):
  36. s = pickle.dumps(ct)
  37. with ctime("Time spent unpickling ctable with N=%d" % N):
  38. ct2 = pickle.loads(s)
  39. np.testing.assert_allclose(ct2['a'][:], a)
  40. np.testing.assert_allclose(ct2['b'][:], b)