From cf55cfd76f0088a61088169508cc1d014f323875 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Mon, 5 Aug 2019 19:12:25 +0200 Subject: [PATCH] Tiny speedup when listing tags Noticed that the code was originally concatenating strings just to then append the result to another string. Now it does not create temporaries and also preallocates the string buffer. --- include/internal/catch_list.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/include/internal/catch_list.cpp b/include/internal/catch_list.cpp index e1d99222..11e55140 100644 --- a/include/internal/catch_list.cpp +++ b/include/internal/catch_list.cpp @@ -84,9 +84,18 @@ namespace Catch { } std::string TagInfo::all() const { - std::string out; - for( auto const& spelling : spellings ) - out += "[" + spelling + "]"; + size_t size = 0; + for (auto const& spelling : spellings) { + // Add 2 for the brackes + size += spelling.size() + 2; + } + + std::string out; out.reserve(size); + for (auto const& spelling : spellings) { + out += '['; + out += spelling; + out += ']'; + } return out; }