2018-01-22 22:38:48 +08:00
|
|
|
// Copyright (c) 2017-2018 Thomas Fussell
|
2017-04-14 08:18:32 +08:00
|
|
|
//
|
|
|
|
// 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
|
2021-08-22 20:23:18 +08:00
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
2017-04-14 08:18:32 +08:00
|
|
|
// 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>
|
2017-04-14 08:18:32 +08:00
|
|
|
|
|
|
|
#include <helpers/timing.hpp>
|
2016-02-06 23:04:41 +08:00
|
|
|
#include <xlnt/xlnt.hpp>
|
|
|
|
|
2017-04-14 08:18:32 +08:00
|
|
|
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++)
|
|
|
|
{
|
2018-07-29 06:10:36 +08:00
|
|
|
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
|
|
|
|
2017-03-22 07:14:27 +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
|
|
|
}
|
2018-07-29 06:10:36 +08:00
|
|
|
std::cout << '\n';
|
2016-02-06 23:04:41 +08:00
|
|
|
|
2017-04-14 08:18:32 +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;
|
2018-07-29 06:10:36 +08:00
|
|
|
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++)
|
|
|
|
{
|
2018-07-29 06:10:36 +08:00
|
|
|
auto start = std::chrono::high_resolution_clock::now();
|
2016-12-04 19:20:12 +08:00
|
|
|
fn(cols, rows);
|
2018-07-29 06:10:36 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-04-14 08:18:32 +08:00
|
|
|
} // namespace
|
|
|
|
|
2016-02-06 23:04:41 +08:00
|
|
|
int main()
|
|
|
|
{
|
2018-07-29 11:05:21 +08:00
|
|
|
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;
|
|
|
|
}
|