scr.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import os
  2. import time
  3. import numpy as np
  4. import nixio as nix
  5. import matplotlib.pyplot as plt
  6. from tempfile import TemporaryDirectory
  7. from typing import List, Tuple
  8. def create_file(nixfile: nix.File) -> List[int]:
  9. times = []
  10. blk = nixfile.create_block("benchmark", "block")
  11. grp = blk.create_group("benchmark", "group")
  12. for idx in range(200):
  13. da = blk.create_data_array(name=str(idx), array_type="array", data=[0])
  14. t0 = time.time()
  15. grp.data_arrays.append(da)
  16. times.append(time.time() - t0)
  17. return times
  18. def benchmark_retrieve_actual(nixfile: nix.File) -> Tuple[List[int], List[int], List[int]]:
  19. create_file(nixfile)
  20. by_name = []
  21. by_id = []
  22. by_idx = []
  23. blk = nixfile.blocks[0]
  24. for idx, da in enumerate(blk.data_arrays):
  25. t0 = time.time()
  26. blk.data_arrays[da.name]
  27. by_name.append(time.time() - t0)
  28. t0 = time.time()
  29. blk.data_arrays[da.id]
  30. by_id.append(time.time() - t0)
  31. t0 = time.time()
  32. blk.data_arrays[idx]
  33. by_idx.append(time.time() - t0)
  34. return by_name, by_id, by_idx
  35. def benchmark_retrieve_link(nixfile: nix.File) -> Tuple[List[int], List[int], List[int]]:
  36. create_file(nixfile)
  37. by_name = []
  38. by_id = []
  39. by_idx = []
  40. blk = nixfile.blocks[0]
  41. grp = blk.groups[0]
  42. for idx, da in enumerate(grp.data_arrays):
  43. t0 = time.time()
  44. grp.data_arrays[da.name]
  45. by_name.append(time.time() - t0)
  46. t0 = time.time()
  47. grp.data_arrays[da.id]
  48. by_id.append(time.time() - t0)
  49. t0 = time.time()
  50. grp.data_arrays[idx]
  51. by_idx.append(time.time() - t0)
  52. return by_name, by_id, by_idx
  53. def main():
  54. with TemporaryDirectory() as tmpdir:
  55. with nix.File(os.path.join(tmpdir, "retrieve"), nix.FileMode.Overwrite) as nixfile:
  56. by_name, by_id, by_idx = benchmark_retrieve_actual(nixfile)
  57. plt.figure("Retrieval times from block.data_arrays")
  58. plt.plot(by_name, ".", label="by name")
  59. plt.plot(by_id, ".", label="by ID")
  60. plt.plot(by_idx, ".", label="by index")
  61. plt.legend()
  62. plt.xlabel("Item index")
  63. plt.ylabel("Time (s)")
  64. with TemporaryDirectory() as tmpdir:
  65. with nix.File(os.path.join(tmpdir, "link-retrieve"), nix.FileMode.Overwrite) as nixfile:
  66. by_name, by_id, by_idx = benchmark_retrieve_link(nixfile)
  67. plt.figure("Retrieval times from group.data_arrays")
  68. plt.plot(by_name, ".", label="by name")
  69. plt.plot(by_id, ".", label="by ID")
  70. plt.plot(by_idx, ".", label="by index")
  71. plt.legend()
  72. plt.xlabel("Item index")
  73. plt.ylabel("Time (s)")
  74. plt.show()
  75. if __name__ == "__main__":
  76. main()