xlnt/benchmarks/writer.cpp

76 lines
1.8 KiB
C++
Raw Normal View History

2016-02-06 23:04:41 +08:00
#include <chrono>
2016-12-03 23:31:30 +08:00
#include <iostream>
2016-02-06 23:04:41 +08:00
#include <xlnt/xlnt.hpp>
2016-12-04 19:20:12 +08:00
std::size_t current_time()
2016-02-06 23:04:41 +08:00
{
return std::chrono::duration<double, std::milli>(std::chrono::system_clock::now().time_since_epoch()).count();
}
// 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.
2016-12-04 19:20:12 +08:00
void writer(int cols, int rows)
2016-02-06 23:04:41 +08:00
{
xlnt::workbook wb;
auto ws = wb.create_sheet();
std::vector<int> row;
for(int i = 0; i < cols; i++)
{
2016-12-04 19:20:12 +08:00
row.push_back(i);
2016-02-06 23:04:41 +08:00
}
for(int index = 0; index < rows; index++)
{
2016-12-04 19:20:12 +08:00
if ((index + 1) % (rows / 10) == 0)
{
std::string progress = std::string((index + 1) / (1 + rows / 10), '.');
std::cout << "\r" << progress;
std::cout.flush();
}
2016-02-06 23:04:41 +08:00
ws.append(row);
}
std::cout << std::endl;
2016-12-04 19:20:12 +08:00
auto filename = "data/benchmark.xlsx";
2016-02-06 23:04:41 +08:00
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.
2016-12-04 19:20:12 +08:00
int timer(std::function<void(int, int)> fn, int cols, int rows)
2016-02-06 23:04:41 +08:00
{
2016-12-04 19:20:12 +08:00
const auto repeat = std::size_t(3);
auto time = std::numeric_limits<std::size_t>::max();
2016-02-06 23:04:41 +08:00
2016-12-04 19:20:12 +08:00
std::cout << cols << " cols " << rows << " rows" << std::endl;
2016-02-06 23:04:41 +08:00
2016-12-04 19:20:12 +08:00
for(int i = 0; i < repeat; i++)
{
auto start = current_time();
fn(cols, rows);
time = std::min(current_time() - start, time);
2016-02-06 23:04:41 +08:00
}
2016-12-04 19:20:12 +08:00
std::cout << time / 1000.0 << std::endl;
2016-02-06 23:04:41 +08:00
2016-12-04 19:20:12 +08:00
return time;
2016-02-06 23:04:41 +08:00
}
int main()
{
timer(&writer, 100, 100);
timer(&writer, 1000, 100);
timer(&writer, 4000, 100);
timer(&writer, 8192, 100);
timer(&writer, 10, 10000);
timer(&writer, 4000, 1000);
return 0;
}