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 <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);
}
@ -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();
@ -36,36 +36,30 @@ void writer(bool optimized, int cols, int rows)
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, Worksheet is " << (opt ? "optimised" : "not optimised") << std::endl;
auto &time = opt ? min_time_optimized : min_time_standard;
std::cout << cols << " cols " << rows << " rows" << std::endl;
for(int i = 0; i < repeat; i++)
{
auto start = current_time();
fn(opt, cols, rows);
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()