reader.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import os
  2. import sys
  3. import timeit
  4. import openpyxl
  5. def reader(optimised):
  6. """
  7. Loop through all cells of a workbook
  8. """
  9. folder = os.path.split(__file__)[0]
  10. src = os.path.join(folder, "files", "very_large.xlsx")
  11. wb = openpyxl.load_workbook(src, use_iterators=optimised)
  12. ws = wb.active
  13. rows = ws.iter_rows()
  14. for r, row in enumerate(rows):
  15. for c, col in enumerate(row):
  16. pass
  17. print((r+1)* (c+1), "cells")
  18. def timer(fn):
  19. """
  20. Create a timeit call to a function and pass in keyword arguments.
  21. The function is called twice, once using the standard workbook, then with the optimised one.
  22. Time from the best of three is taken.
  23. """
  24. print("lxml", openpyxl.LXML)
  25. result = []
  26. for opt in (False, True,):
  27. print("Workbook is {0}".format(opt and "optimised" or "not optimised"))
  28. times = timeit.repeat("{0}({1})".format(fn.__name__, opt),
  29. setup="from __main__ import {0}".format(fn.__name__),
  30. number = 1,
  31. repeat = 3
  32. )
  33. print("{0:.2f}s".format(min(times)))
  34. result.append(min(times))
  35. std, opt = result
  36. print("Optimised takes {0:.2%} time\n".format(opt/std))
  37. return std, opt
  38. if __name__ == "__main__":
  39. timer(reader)