diff options
| author | Mehmet Samet Duman <yongdohyun@projecttick.org> | 2026-04-02 18:44:05 +0300 |
|---|---|---|
| committer | Mehmet Samet Duman <yongdohyun@projecttick.org> | 2026-04-02 18:44:05 +0300 |
| commit | 0b24459ac12b6cf9fd5a401d647796ca254a8fa8 (patch) | |
| tree | f2fd66e2476976a51e2a51330fd95dc6e87b24c1 /tomlplusplus/examples/parse_benchmark.cpp | |
| parent | b85e90fc3480da0e6a48da73201a0b22488cc650 (diff) | |
| parent | 1c8b7466e4946fcc3bf20484c0e1d001202cca5a (diff) | |
| download | Project-Tick-0b24459ac12b6cf9fd5a401d647796ca254a8fa8.tar.gz Project-Tick-0b24459ac12b6cf9fd5a401d647796ca254a8fa8.zip | |
Add 'tomlplusplus/' from commit '1c8b7466e4946fcc3bf20484c0e1d001202cca5a'
git-subtree-dir: tomlplusplus
git-subtree-mainline: b85e90fc3480da0e6a48da73201a0b22488cc650
git-subtree-split: 1c8b7466e4946fcc3bf20484c0e1d001202cca5a
Diffstat (limited to 'tomlplusplus/examples/parse_benchmark.cpp')
| -rw-r--r-- | tomlplusplus/examples/parse_benchmark.cpp | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/tomlplusplus/examples/parse_benchmark.cpp b/tomlplusplus/examples/parse_benchmark.cpp new file mode 100644 index 0000000000..8c2b53d92e --- /dev/null +++ b/tomlplusplus/examples/parse_benchmark.cpp @@ -0,0 +1,81 @@ +// This file is a part of toml++ and is subject to the the terms of the MIT license. +// Copyright (c) Mark Gillard <mark.gillard@outlook.com.au> +// See https://github.com/marzer/tomlplusplus/blob/master/LICENSE for the full license text. +// SPDX-License-Identifier: MIT + +// This example is just a short-n-shiny benchmark. + +#include "examples.hpp" +#include <toml++/toml.hpp> + +using namespace std::string_view_literals; + +static constexpr size_t iterations = 10000; + +int main(int argc, char** argv) +{ + const auto file_path = std::string(argc > 1 ? std::string_view{ argv[1] } : "benchmark_data.toml"sv); + + // read the file into a string first to remove file I/O from the benchmark + std::string file_content; + { + std::ifstream file(file_path, std::ifstream::in | std::ifstream::binary | std::ifstream::ate); + if (!file) + { + std::cerr << "File '"sv << file_path << "'could not be opened for reading\n"sv; + return -1; + } + + const auto file_size = file.tellg(); + if (file_size == -1) + { + std::cerr << "File '"sv << file_path << "' could not be opened for reading\n"sv; + return -1; + } + file.seekg(0, std::ifstream::beg); + + file_content.resize(static_cast<size_t>(file_size)); + file.read(file_content.data(), static_cast<std::streamsize>(file_size)); + if (!file.eof() && !file) + { + std::cerr << "Failed to read contents of file '"sv << file_path << "'\n"sv; + return -1; + } + } + + // parse once to make sure it isn't garbage + { +#if TOML_EXCEPTIONS + try + { + const auto result = toml::parse(file_content, file_path); + } + catch (const toml::parse_error& err) + { + std::cerr << err << "\n"; + return 1; + } +#else + const auto result = toml::parse(file_content, file_path); + if (!result) + { + std::cerr << result.error() << "\n"; + return 1; + } +#endif + } + + // run the benchmark + std::cout << "Parsing '"sv << file_path << "' "sv << iterations << " times...\n"sv; + + const auto start = std::chrono::steady_clock::now(); + for (size_t i = 0; i < iterations; i++) + std::ignore = toml::parse(file_content, file_path); + const auto cumulative_sec = + std::chrono::duration_cast<std::chrono::duration<double>>(std::chrono::steady_clock::now() - start).count(); + const auto mean_sec = cumulative_sec / static_cast<double>(iterations); + std::cout << " total: "sv << cumulative_sec << " s\n"sv + << " mean: "sv << mean_sec << " s\n"sv; + + return 0; +} |
