diff --git a/benchmarks/styles.cpp b/benchmarks/styles.cpp index 4642ffef..ae24d8db 100644 --- a/benchmarks/styles.cpp +++ b/benchmarks/styles.cpp @@ -1,14 +1,20 @@ +#include #include #include #include #include +std::size_t current_time() +{ + return std::chrono::duration(std::chrono::system_clock::now().time_since_epoch()).count(); +} + std::size_t random_index(std::size_t max) { static std::random_device rd; static std::mt19937 gen(rd()); - std::uniform_int_distribution<> dis(0, max); + std::uniform_int_distribution<> dis(0, max - 1); return dis(gen); } @@ -40,7 +46,7 @@ std::vector generate_all_styles(xlnt::workbook &wb) { for(auto italic : italic_options) { - auto s = wb.create_style(std::to_string(index++)); + auto s = wb.create_style(std::to_string(index++)); xlnt::font f; f.name(name); @@ -74,10 +80,10 @@ xlnt::workbook non_optimized_workbook(int n) for(int idx = 1; idx < n; idx++) { - auto worksheet = wb[random_index(wb.sheet_count())]; + auto worksheet = wb[random_index(wb.sheet_count())]; auto cell = worksheet.cell(1, (xlnt::row_t)idx); cell.value(0); - cell.style(styles.at(random_index(styles.size()))); + cell.style(styles.at(random_index(styles.size()))); } return wb; @@ -85,9 +91,10 @@ xlnt::workbook non_optimized_workbook(int n) void to_profile(xlnt::workbook &wb, const std::string &f, int n) { - auto t = 0;//-time.time(); + auto start = current_time(); wb.save(f); - std::cout << "took " << t << "s for " << n << " styles"; + auto elapsed = current_time() - start; + std::cout << "took " << elapsed / 1000.0 << "s for " << n << " styles" << std::endl; } int main() @@ -96,4 +103,6 @@ int main() auto wb = non_optimized_workbook(n); std::string f = "temp.xlsx"; to_profile(wb, f, n); + + return 0; } diff --git a/benchmarks/writer.cpp b/benchmarks/writer.cpp index 49323635..05fc08e8 100644 --- a/benchmarks/writer.cpp +++ b/benchmarks/writer.cpp @@ -2,7 +2,7 @@ #include #include -int current_time() +std::size_t current_time() { return std::chrono::duration(std::chrono::system_clock::now().time_since_epoch()).count(); } @@ -10,7 +10,7 @@ int current_time() // Create a worksheet with variable width rows. Because data must be // serialised row by row it is often the width of the rows which is most // important. -void writer(bool optimized, int cols, int rows) +void writer(int cols, int rows) { xlnt::workbook wb; auto ws = wb.create_sheet(); @@ -19,53 +19,47 @@ void writer(bool optimized, int cols, int rows) for(int i = 0; i < cols; i++) { - row.push_back(i); + row.push_back(i); } for(int index = 0; index < rows; index++) { - if ((index + 1) % (rows / 10) == 0) - { - std::string progress = std::string((index + 1) / (1 + rows / 10), '.'); - std::cout << "\r" << progress; - std::cout.flush(); - } + if ((index + 1) % (rows / 10) == 0) + { + std::string progress = std::string((index + 1) / (1 + rows / 10), '.'); + std::cout << "\r" << progress; + std::cout.flush(); + } ws.append(row); } std::cout << std::endl; - auto filename = "data/large.xlsx"; + auto filename = "data/benchmark.xlsx"; wb.save(filename); } // Create a timeit call to a function and pass in keyword arguments. // The function is called twice, once using the standard workbook, then with the optimised one. // Time from the best of three is taken. -std::pair timer(std::function fn, int cols, int rows) +int timer(std::function fn, int cols, int rows) { - const int repeat = 3; - int min_time_standard = std::numeric_limits::max(); - int min_time_optimized = std::numeric_limits::max(); + const auto repeat = std::size_t(3); + auto time = std::numeric_limits::max(); - for(bool opt : {false, true}) + std::cout << cols << " cols " << rows << " rows" << std::endl; + + for(int i = 0; i < repeat; i++) { - std::cout << cols << " cols " << rows << " rows, Worksheet is " << (opt ? "optimised" : "not optimised") << std::endl; - auto &time = opt ? min_time_optimized : min_time_standard; - - for(int i = 0; i < repeat; i++) - { - auto start = current_time(); - fn(opt, cols, rows); - time = std::min(current_time() - start, time); - } + auto start = current_time(); + fn(cols, rows); + time = std::min(current_time() - start, time); } - double ratio = min_time_optimized / static_cast(min_time_standard) * 100; - std::cout << "Optimised takes " << ratio << "% time" << std::endl; + std::cout << time / 1000.0 << std::endl; - return {min_time_standard, min_time_optimized}; + return time; } int main()