xlnt/benchmarks/writer.cpp

91 lines
2.9 KiB
C++
Raw Normal View History

2018-01-22 22:38:48 +08:00
// Copyright (c) 2017-2018 Thomas Fussell
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE
//
// @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file
2016-02-06 23:04:41 +08:00
#include <chrono>
2016-12-03 23:31:30 +08:00
#include <iostream>
#include <helpers/timing.hpp>
2016-02-06 23:04:41 +08:00
#include <xlnt/xlnt.hpp>
namespace {
2016-02-06 23:04:41 +08:00
// 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();
for(int index = 0; index < rows; index++)
{
if (rows >= 10 && (index + 1) % (rows / 10) == 0)
2016-12-04 19:20:12 +08:00
{
std::string progress = std::string((index + 1) / (1 + rows / 10), '.');
std::cout << "\r" << progress;
}
2016-02-06 23:04:41 +08:00
for (int i = 0; i < cols; i++)
{
ws.cell(xlnt::cell_reference(i + 1, index + 1)).value(i);
}
2016-02-06 23:04:41 +08:00
}
std::cout << '\n';
2016-02-06 23:04:41 +08:00
auto filename = "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.
2017-04-14 07:01:30 +08:00
void 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);
2018-07-29 11:11:19 +08:00
std::chrono::duration<double, std::milli> time{};
2016-12-04 19:20:12 +08:00
std::cout << cols << " cols " << rows << " rows" << std::endl;
fn(rows, cols); // 1 cold run
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 = std::chrono::high_resolution_clock::now();
2016-12-04 19:20:12 +08:00
fn(cols, rows);
time += std::chrono::high_resolution_clock::now() - start;
2016-02-06 23:04:41 +08:00
}
2018-07-29 11:11:19 +08:00
std::cout << time.count() / repeat << " ms per iteration" << '\n' << '\n';
2016-02-06 23:04:41 +08:00
}
} // namespace
2016-02-06 23:04:41 +08:00
int main()
{
timer(&writer, 10000, 1);
timer(&writer, 1000, 10);
timer(&writer, 100, 100);
timer(&writer, 10, 1000);
timer(&writer, 1, 10000);
2016-02-06 23:04:41 +08:00
return 0;
}