mirror of
https://github.com/tfussell/xlnt.git
synced 2024-03-22 13:11:17 +08:00
Test of row/columns generation with random format.
This commit is contained in:
parent
2459b336ef
commit
1aac106a40
|
@ -22,7 +22,9 @@
|
||||||
// @author: see AUTHORS file
|
// @author: see AUTHORS file
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
#include <string>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
#include <random>
|
#include <random>
|
||||||
|
|
||||||
|
@ -41,98 +43,6 @@ std::size_t random_index(std::size_t max)
|
||||||
return dis(gen);
|
return dis(gen);
|
||||||
}
|
}
|
||||||
|
|
||||||
void generate_all_styles(xlnt::workbook &wb, std::vector<xlnt::style>& styles)
|
|
||||||
{
|
|
||||||
const auto vertical_alignments = std::vector<xlnt::vertical_alignment>
|
|
||||||
{
|
|
||||||
xlnt::vertical_alignment::center,
|
|
||||||
xlnt::vertical_alignment::justify,
|
|
||||||
xlnt::vertical_alignment::top,
|
|
||||||
xlnt::vertical_alignment::bottom
|
|
||||||
};
|
|
||||||
|
|
||||||
const auto horizontal_alignments = std::vector<xlnt::horizontal_alignment>
|
|
||||||
{
|
|
||||||
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
|
|
||||||
};
|
|
||||||
|
|
||||||
const auto font_names = std::vector<std::string>
|
|
||||||
{
|
|
||||||
"Calibri",
|
|
||||||
"Tahoma",
|
|
||||||
"Arial",
|
|
||||||
"Times New Roman"
|
|
||||||
};
|
|
||||||
|
|
||||||
const auto font_sizes = std::vector<double>
|
|
||||||
{
|
|
||||||
11.,
|
|
||||||
13.,
|
|
||||||
15.,
|
|
||||||
17.,
|
|
||||||
19.,
|
|
||||||
21.,
|
|
||||||
23.,
|
|
||||||
25.,
|
|
||||||
27.,
|
|
||||||
29.,
|
|
||||||
31.,
|
|
||||||
33.,
|
|
||||||
35.
|
|
||||||
};
|
|
||||||
|
|
||||||
const auto underline_options = std::vector<xlnt::font::underline_style>
|
|
||||||
{
|
|
||||||
xlnt::font::underline_style::single,
|
|
||||||
xlnt::font::underline_style::none
|
|
||||||
};
|
|
||||||
|
|
||||||
std::size_t index = 0;
|
|
||||||
|
|
||||||
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 : { true, false })
|
|
||||||
{
|
|
||||||
for (auto underline : underline_options)
|
|
||||||
{
|
|
||||||
for (auto italic : { true, false })
|
|
||||||
{
|
|
||||||
auto s = wb.create_style(std::to_string(index++));
|
|
||||||
|
|
||||||
xlnt::font f;
|
|
||||||
f.name(name);
|
|
||||||
f.size(size);
|
|
||||||
f.italic(italic);
|
|
||||||
f.underline(underline);
|
|
||||||
f.bold(bold);
|
|
||||||
s.font(f);
|
|
||||||
|
|
||||||
xlnt::alignment a;
|
|
||||||
a.vertical(vertical_alignment);
|
|
||||||
a.horizontal(horizontal_alignment);
|
|
||||||
s.alignment(a);
|
|
||||||
|
|
||||||
styles.push_back(s);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void generate_all_formats(xlnt::workbook &wb, std::vector<xlnt::format>& formats)
|
void generate_all_formats(xlnt::workbook &wb, std::vector<xlnt::format>& formats)
|
||||||
{
|
{
|
||||||
const auto vertical_alignments = std::vector<xlnt::vertical_alignment>
|
const auto vertical_alignments = std::vector<xlnt::vertical_alignment>
|
||||||
|
@ -184,8 +94,6 @@ void generate_all_formats(xlnt::workbook &wb, std::vector<xlnt::format>& formats
|
||||||
xlnt::font::underline_style::none
|
xlnt::font::underline_style::none
|
||||||
};
|
};
|
||||||
|
|
||||||
std::size_t index = 0;
|
|
||||||
|
|
||||||
for (auto vertical_alignment : vertical_alignments)
|
for (auto vertical_alignment : vertical_alignments)
|
||||||
{
|
{
|
||||||
for (auto horizontal_alignment : horizontal_alignments)
|
for (auto horizontal_alignment : horizontal_alignments)
|
||||||
|
@ -225,37 +133,8 @@ void generate_all_formats(xlnt::workbook &wb, std::vector<xlnt::format>& formats
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
xlnt::workbook non_optimized_workbook_styles(int n)
|
|
||||||
{
|
|
||||||
using xlnt::benchmarks::current_time;
|
|
||||||
|
|
||||||
xlnt::workbook wb;
|
xlnt::workbook non_optimized_workbook_formats(int rows_number, int columns_number)
|
||||||
std::vector<xlnt::style> styles;
|
|
||||||
auto start = current_time();
|
|
||||||
|
|
||||||
generate_all_styles(wb, styles);
|
|
||||||
|
|
||||||
auto elapsed = current_time() - start;
|
|
||||||
|
|
||||||
std::cout << "took " << elapsed / 1000.0 << "s for " << n << " rows. generate_all_styles" << std::endl;
|
|
||||||
|
|
||||||
start = current_time();
|
|
||||||
for(int idx = 1; idx < n; idx++)
|
|
||||||
{
|
|
||||||
auto worksheet = wb[random_index(wb.sheet_count())];
|
|
||||||
auto cell = worksheet.cell(xlnt::cell_reference(1, (xlnt::row_t)idx));
|
|
||||||
cell.value(idx);
|
|
||||||
cell.style(styles.at(random_index(styles.size())));
|
|
||||||
}
|
|
||||||
|
|
||||||
elapsed = current_time() - start;
|
|
||||||
|
|
||||||
std::cout << "took " << elapsed / 1000.0 << "s for " << n << " rows. set values and styles for cells" << std::endl;
|
|
||||||
|
|
||||||
return wb;
|
|
||||||
}
|
|
||||||
|
|
||||||
xlnt::workbook non_optimized_workbook_formats(int n)
|
|
||||||
{
|
{
|
||||||
using xlnt::benchmarks::current_time;
|
using xlnt::benchmarks::current_time;
|
||||||
|
|
||||||
|
@ -267,20 +146,26 @@ xlnt::workbook non_optimized_workbook_formats(int n)
|
||||||
|
|
||||||
auto elapsed = current_time() - start;
|
auto elapsed = current_time() - start;
|
||||||
|
|
||||||
std::cout << "took " << elapsed / 1000.0 << "s for " << n << " rows. generate_all_formats" << std::endl;
|
std::cout << "took " << elapsed / 1000.0 << "s for " << rows_number << " rows. generate_all_formats, number of unique formats " << formats.size() << std::endl;
|
||||||
|
|
||||||
start = current_time();
|
start = current_time();
|
||||||
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(xlnt::cell_reference(1, (xlnt::row_t)idx));
|
|
||||||
cell.value(idx);
|
for (int row_idx = 1; row_idx < rows_number; row_idx++)
|
||||||
|
{
|
||||||
|
for (int col_idx = 1; col_idx < columns_number; col_idx++)
|
||||||
|
{
|
||||||
|
auto cell = worksheet.cell(xlnt::cell_reference((xlnt::column_t)col_idx, (xlnt::row_t)row_idx));
|
||||||
|
std::ostringstream string_stm;
|
||||||
|
string_stm << "Col: " << col_idx << "Row: " << row_idx;
|
||||||
|
cell.value(string_stm.str());
|
||||||
cell.format(formats.at(random_index(formats.size())));
|
cell.format(formats.at(random_index(formats.size())));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
elapsed = current_time() - start;
|
elapsed = current_time() - start;
|
||||||
|
|
||||||
std::cout << "took " << elapsed / 1000.0 << "s for " << n << " rows. set values and formats for cells" << std::endl;
|
std::cout << "took " << elapsed / 1000.0 << "s for " << rows_number << " rows. set values and formats for cells" << std::endl;
|
||||||
|
|
||||||
return wb;
|
return wb;
|
||||||
}
|
}
|
||||||
|
@ -307,26 +192,6 @@ void to_load_profile(xlnt::workbook &wb, const std::string &f, int n, const std:
|
||||||
std::cout << "took " << elapsed / 1000.0 << "s for " << n << " rows. load workbook with " << msg << std::endl;
|
std::cout << "took " << elapsed / 1000.0 << "s for " << n << " rows. load workbook with " << msg << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void read_styles_profile(xlnt::workbook &wb, int n)
|
|
||||||
{
|
|
||||||
using xlnt::benchmarks::current_time;
|
|
||||||
|
|
||||||
std::vector<int> values;
|
|
||||||
std::vector<xlnt::style> styles;
|
|
||||||
auto start = current_time();
|
|
||||||
for (int idx = 1; idx < n; idx++)
|
|
||||||
{
|
|
||||||
auto worksheet = wb[random_index(wb.sheet_count())];
|
|
||||||
auto cell = worksheet.cell(xlnt::cell_reference(1, (xlnt::row_t)idx));
|
|
||||||
values.push_back(cell.value<int>());
|
|
||||||
styles.push_back(cell.style());
|
|
||||||
}
|
|
||||||
|
|
||||||
auto elapsed = current_time() - start;
|
|
||||||
|
|
||||||
std::cout << "took " << elapsed / 1000.0 << "s for " << n << " rows. read values and styles for cells" << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
void read_formats_profile(xlnt::workbook &wb, int n)
|
void read_formats_profile(xlnt::workbook &wb, int n)
|
||||||
{
|
{
|
||||||
using xlnt::benchmarks::current_time;
|
using xlnt::benchmarks::current_time;
|
||||||
|
@ -349,28 +214,32 @@ void read_formats_profile(xlnt::workbook &wb, int n)
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
int main()
|
int main(int argc, char * argv[])
|
||||||
{
|
{
|
||||||
int n = 10000;
|
int rows_number = 10000;
|
||||||
|
int columns_number = 100;
|
||||||
|
|
||||||
std::cout << "started. number of rows for styles " << n << std::endl;
|
try
|
||||||
|
{
|
||||||
|
if (argc > 1)
|
||||||
|
rows_number = std::stoi(argv[1]);
|
||||||
|
|
||||||
auto wb = non_optimized_workbook_styles(n);
|
if (argc > 2)
|
||||||
std::string f = "temp-styles.xlsx";
|
columns_number = std::stoi(argv[2]);
|
||||||
to_save_profile(wb, f, n, "styles");
|
|
||||||
|
|
||||||
xlnt::workbook load_styles_wb;
|
std::cout << "started. number of rows " << rows_number << ", number of columns " << columns_number << std::endl;
|
||||||
to_load_profile(load_styles_wb, f, n, "styles");
|
auto wb = non_optimized_workbook_formats(rows_number, columns_number);
|
||||||
read_styles_profile(load_styles_wb, n);
|
auto f = "temp-formats.xlsx";
|
||||||
|
to_save_profile(wb, f, rows_number, "formats");
|
||||||
std::cout << "started. number of rows for formats " << n << std::endl;
|
|
||||||
wb = non_optimized_workbook_formats(n);
|
|
||||||
f = "temp-formats.xlsx";
|
|
||||||
to_save_profile(wb, f, n, "formats");
|
|
||||||
|
|
||||||
xlnt::workbook load_formats_wb;
|
xlnt::workbook load_formats_wb;
|
||||||
to_load_profile(load_formats_wb, f, n, "formats");
|
to_load_profile(load_formats_wb, f, rows_number, "formats");
|
||||||
read_formats_profile(load_formats_wb, n);
|
read_formats_profile(load_formats_wb, rows_number);
|
||||||
|
}
|
||||||
|
catch(std::exception& ex)
|
||||||
|
{
|
||||||
|
std::cout << "failed. " << ex.what() << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user