From 9aa11b4a003401822f78614272b83427ba08f124 Mon Sep 17 00:00:00 2001 From: Timo Koch Date: Wed, 15 Aug 2018 02:48:36 +0200 Subject: [PATCH] [io][container] Add optional argument to preallocate memory for efficient reading --- dumux/io/container.hh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/dumux/io/container.hh b/dumux/io/container.hh index fd4a17e3af..9d8d68a810 100644 --- a/dumux/io/container.hh +++ b/dumux/io/container.hh @@ -52,14 +52,16 @@ void writeContainerToFile(const Container& v, /*! * \brief Read a simple text file into a container * \param filename The filename to write to + * \param sizeEstimate an estimate on how many items are there to read into the container * \tparam Container The container type, requires begin(), end(), push_back() method * * usage: auto v = readFileToContainer>("myvector.txt"); */ template -Container readFileToContainer(const std::string& filename) +Container readFileToContainer(const std::string& filename, std::size_t sizeEstimate = 0) { Container v; + if (sizeEstimate > 0) v.reserve(sizeEstimate); // reserving memory can significantly speed up the reading std::ifstream infile(filename, std::ios::in); std::istream_iterator it(infile); std::copy(it, std::istream_iterator(), std::back_inserter(v)); -- GitLab