make benchmarks show something useful

pull/101/head
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 <iterator>
#include <random>
#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)
{
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<xlnt::style> 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;
}

View File

@ -2,7 +2,7 @@
#include <iostream>
#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();
}
@ -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<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;
int min_time_standard = std::numeric_limits<int>::max();
int min_time_optimized = std::numeric_limits<int>::max();
const auto repeat = std::size_t(3);
auto time = std::numeric_limits<std::size_t>::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<double>(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()