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);
} }
@ -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();
@ -36,36 +36,30 @@ void writer(bool optimized, int cols, int rows)
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;
{
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++) for(int i = 0; i < repeat; i++)
{ {
auto start = current_time(); auto start = current_time();
fn(opt, cols, rows); fn(cols, rows);
time = std::min(current_time() - start, time); 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()