writer.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright (c) 2017-2018 Thomas Fussell
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE
  20. //
  21. // @license: http://www.opensource.org/licenses/mit-license.php
  22. // @author: see AUTHORS file
  23. #include <chrono>
  24. #include <iostream>
  25. #include <helpers/timing.hpp>
  26. #include <xlnt/xlnt.hpp>
  27. namespace {
  28. // Create a worksheet with variable width rows. Because data must be
  29. // serialised row by row it is often the width of the rows which is most
  30. // important.
  31. void writer(int cols, int rows)
  32. {
  33. xlnt::workbook wb;
  34. auto ws = wb.create_sheet();
  35. for(int index = 0; index < rows; index++)
  36. {
  37. if (rows >= 10 && (index + 1) % (rows / 10) == 0)
  38. {
  39. std::string progress = std::string((index + 1) / (1 + rows / 10), '.');
  40. std::cout << "\r" << progress;
  41. }
  42. for (int i = 0; i < cols; i++)
  43. {
  44. ws.cell(xlnt::cell_reference(i + 1, index + 1)).value(i);
  45. }
  46. }
  47. std::cout << '\n';
  48. auto filename = "benchmark.xlsx";
  49. wb.save(filename);
  50. }
  51. // Create a timeit call to a function and pass in keyword arguments.
  52. // The function is called twice, once using the standard workbook, then with the optimised one.
  53. // Time from the best of three is taken.
  54. void timer(std::function<void(int, int)> fn, int cols, int rows)
  55. {
  56. const auto repeat = std::size_t(3);
  57. std::chrono::duration<double, std::milli> time{};
  58. std::cout << cols << " cols " << rows << " rows" << std::endl;
  59. fn(rows, cols); // 1 cold run
  60. for(int i = 0; i < repeat; i++)
  61. {
  62. auto start = std::chrono::high_resolution_clock::now();
  63. fn(cols, rows);
  64. time += std::chrono::high_resolution_clock::now() - start;
  65. }
  66. std::cout << time.count() / repeat << " ms per iteration" << '\n' << '\n';
  67. }
  68. } // namespace
  69. int main()
  70. {
  71. timer(&writer, 10000, 1);
  72. timer(&writer, 1000, 10);
  73. timer(&writer, 100, 100);
  74. timer(&writer, 10, 1000);
  75. timer(&writer, 1, 10000);
  76. return 0;
  77. }