xlnt/benchmarks/styles.cpp

109 lines
3.6 KiB
C++
Raw Normal View History

2016-12-04 19:20:12 +08:00
#include <chrono>
2016-12-03 23:31:30 +08:00
#include <iostream>
2016-02-06 23:04:41 +08:00
#include <iterator>
#include <random>
#include <xlnt/xlnt.hpp>
2016-12-04 19:20:12 +08:00
std::size_t current_time()
{
return static_cast<std::size_t>(std::chrono::duration<double, std::milli>(std::chrono::system_clock::now().time_since_epoch()).count());
2016-12-04 19:20:12 +08:00
}
2016-12-03 23:31:30 +08:00
std::size_t random_index(std::size_t max)
{
2016-02-06 23:04:41 +08:00
static std::random_device rd;
static std::mt19937 gen(rd());
2016-12-04 19:20:12 +08:00
std::uniform_int_distribution<> dis(0, max - 1);
2016-02-06 23:04:41 +08:00
2016-12-03 23:31:30 +08:00
return dis(gen);
2016-02-06 23:04:41 +08:00
}
2016-12-03 23:31:30 +08:00
std::vector<xlnt::style> generate_all_styles(xlnt::workbook &wb)
2016-02-06 23:04:41 +08:00
{
std::vector<xlnt::style> styles;
std::vector<xlnt::vertical_alignment> vertical_alignments = {xlnt::vertical_alignment::center, xlnt::vertical_alignment::justify, xlnt::vertical_alignment::top, xlnt::vertical_alignment::bottom};
std::vector<xlnt::horizontal_alignment> horizontal_alignments = {xlnt::horizontal_alignment::center, xlnt::horizontal_alignment::center_continuous, xlnt::horizontal_alignment::general, xlnt::horizontal_alignment::justify, xlnt::horizontal_alignment::left, xlnt::horizontal_alignment::right};
std::vector<std::string> font_names = {"Calibri", "Tahoma", "Arial", "Times New Roman"};
std::vector<int> font_sizes = {11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35};
std::vector<bool> bold_options = {true, false};
std::vector<xlnt::font::underline_style> underline_options = {xlnt::font::underline_style::single, xlnt::font::underline_style::none};
std::vector<bool> italic_options = {true, false};
2016-12-03 23:31:30 +08:00
std::size_t index = 0;
2016-02-06 23:04:41 +08:00
for(auto vertical_alignment : vertical_alignments)
{
for(auto horizontal_alignment : horizontal_alignments)
{
for(auto name : font_names)
{
for(auto size : font_sizes)
{
for(auto bold : bold_options)
{
for(auto underline : underline_options)
{
for(auto italic : italic_options)
{
2016-12-04 19:20:12 +08:00
auto s = wb.create_style(std::to_string(index++));
2016-02-06 23:04:41 +08:00
xlnt::font f;
2016-12-03 23:31:30 +08:00
f.name(name);
f.size(size);
f.italic(italic);
f.underline(underline);
f.bold(bold);
s.font(f);
2016-02-06 23:04:41 +08:00
xlnt::alignment a;
2016-12-03 23:31:30 +08:00
a.vertical(vertical_alignment);
a.horizontal(horizontal_alignment);
s.alignment(a);
2016-02-06 23:04:41 +08:00
styles.push_back(s);
}
}
}
}
}
}
}
return styles;
}
2016-12-03 23:31:30 +08:00
xlnt::workbook non_optimized_workbook(int n)
2016-02-06 23:04:41 +08:00
{
xlnt::workbook wb;
2016-12-03 23:31:30 +08:00
auto styles = generate_all_styles(wb);
2016-02-06 23:04:41 +08:00
for(int idx = 1; idx < n; idx++)
{
2016-12-04 19:20:12 +08:00
auto worksheet = wb[random_index(wb.sheet_count())];
auto cell = worksheet.cell(xlnt::cell_reference(1, (xlnt::row_t)idx));
2016-12-03 23:31:30 +08:00
cell.value(0);
2016-12-04 19:20:12 +08:00
cell.style(styles.at(random_index(styles.size())));
2016-02-06 23:04:41 +08:00
}
return wb;
}
void to_profile(xlnt::workbook &wb, const std::string &f, int n)
{
2016-12-04 19:20:12 +08:00
auto start = current_time();
2016-02-06 23:04:41 +08:00
wb.save(f);
2016-12-04 19:20:12 +08:00
auto elapsed = current_time() - start;
std::cout << "took " << elapsed / 1000.0 << "s for " << n << " styles" << std::endl;
2016-02-06 23:04:41 +08:00
}
int main()
{
int n = 10000;
2016-12-03 23:31:30 +08:00
auto wb = non_optimized_workbook(n);
std::string f = "temp.xlsx";
to_profile(wb, f, n);
2016-12-04 19:20:12 +08:00
return 0;
2016-02-06 23:04:41 +08:00
}