make benchmarks show something useful

This commit is contained in:
Thomas Fussell 2016-12-04 12:20:12 +01:00
parent 39f496f8a4
commit 838f358f34
2 changed files with 36 additions and 33 deletions

View File

@ -1,14 +1,20 @@
#include <chrono>
#include <iostream> #include <iostream>
#include <iterator> #include <iterator>
#include <random> #include <random>
#include <xlnt/xlnt.hpp> #include <xlnt/xlnt.hpp>
std::size_t current_time()
{
return std::chrono::duration<double, std::milli>(std::chrono::system_clock::now().time_since_epoch()).count();
}
std::size_t random_index(std::size_t max) std::size_t random_index(std::size_t max)
{ {
static std::random_device rd; static std::random_device rd;
static std::mt19937 gen(rd()); static std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0, max); std::uniform_int_distribution<> dis(0, max - 1);
return dis(gen); return dis(gen);
} }
@ -40,7 +46,7 @@ std::vector<xlnt::style> generate_all_styles(xlnt::workbook &wb)
{ {
for(auto italic : italic_options) 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; xlnt::font f;
f.name(name); f.name(name);
@ -74,10 +80,10 @@ xlnt::workbook non_optimized_workbook(int n)
for(int idx = 1; idx < n; idx++) 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); auto cell = worksheet.cell(1, (xlnt::row_t)idx);
cell.value(0); cell.value(0);
cell.style(styles.at(random_index(styles.size()))); cell.style(styles.at(random_index(styles.size())));
} }
return wb; 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) void to_profile(xlnt::workbook &wb, const std::string &f, int n)
{ {
auto t = 0;//-time.time(); auto start = current_time();
wb.save(f); 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() int main()
@ -96,4 +103,6 @@ int main()
auto wb = non_optimized_workbook(n); auto wb = non_optimized_workbook(n);
std::string f = "temp.xlsx"; std::string f = "temp.xlsx";
to_profile(wb, f, n); to_profile(wb, f, n);
return 0;
} }

View File

@ -2,7 +2,7 @@
#include <iostream> #include <iostream>
#include <xlnt/xlnt.hpp> #include <xlnt/xlnt.hpp>
int current_time() std::size_t current_time()
{ {
return std::chrono::duration<double, std::milli>(std::chrono::system_clock::now().time_since_epoch()).count(); return std::chrono::duration<double, std::milli>(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 // 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 // serialised row by row it is often the width of the rows which is most
// important. // important.
void writer(bool optimized, int cols, int rows) void writer(int cols, int rows)
{ {
xlnt::workbook wb; xlnt::workbook wb;
auto ws = wb.create_sheet(); auto ws = wb.create_sheet();
@ -19,53 +19,47 @@ void writer(bool optimized, int cols, int rows)
for(int i = 0; i < cols; i++) for(int i = 0; i < cols; i++)
{ {
row.push_back(i); row.push_back(i);
} }
for(int index = 0; index < rows; index++) for(int index = 0; index < rows; index++)
{ {
if ((index + 1) % (rows / 10) == 0) if ((index + 1) % (rows / 10) == 0)
{ {
std::string progress = std::string((index + 1) / (1 + rows / 10), '.'); std::string progress = std::string((index + 1) / (1 + rows / 10), '.');
std::cout << "\r" << progress; std::cout << "\r" << progress;
std::cout.flush(); std::cout.flush();
} }
ws.append(row); ws.append(row);
} }
std::cout << std::endl; std::cout << std::endl;
auto filename = "data/large.xlsx"; auto filename = "data/benchmark.xlsx";
wb.save(filename); wb.save(filename);
} }
// Create a timeit call to a function and pass in keyword arguments. // 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. // The function is called twice, once using the standard workbook, then with the optimised one.
// Time from the best of three is taken. // Time from the best of three is taken.
std::pair<int, int> timer(std::function<void(bool, int, int)> fn, int cols, int rows) int timer(std::function<void(int, int)> fn, int cols, int rows)
{ {
const int repeat = 3; const auto repeat = std::size_t(3);
int min_time_standard = std::numeric_limits<int>::max(); auto time = std::numeric_limits<std::size_t>::max();
int min_time_optimized = std::numeric_limits<int>::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 start = current_time();
auto &time = opt ? min_time_optimized : min_time_standard; fn(cols, rows);
time = std::min(current_time() - start, time);
for(int i = 0; i < repeat; i++)
{
auto start = current_time();
fn(opt, cols, rows);
time = std::min(current_time() - start, time);
}
} }
double ratio = min_time_optimized / static_cast<double>(min_time_standard) * 100; std::cout << time / 1000.0 << std::endl;
std::cout << "Optimised takes " << ratio << "% time" << std::endl;
return {min_time_standard, min_time_optimized}; return time;
} }
int main() int main()