Skip to content

Instantly share code, notes, and snippets.

@mat-tso
Last active March 9, 2016 22:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mat-tso/a27b2a64898a0f2d2a77 to your computer and use it in GitHub Desktop.
Save mat-tso/a27b2a64898a0f2d2a77 to your computer and use it in GitHub Desktop.
Benchmark the build time impact of the inclusion of all c++ containers header and overloading each one with a toString method
#!/bin/env bash
set -euo pipefail
SRC=/tmp/benchmarkInclude.cpp
EXE=/tmp/benshmarkInclude
COMPILERS="clang++ g++"
SCENARIOS="EMPTY_MAIN
INCLUDES EMPTY_MAIN ONLY_VECTOR
INCLUDES EMPTY_MAIN
INCLUDES EMPTY_MAIN ONLY_VECTOR TO_STRING
INCLUDES EMPTY_MAIN TO_STRING
INCLUDES ONLY_VECTOR TO_STRING
INCLUDES TO_STRING"
cat > "$SRC" <<-END
#ifdef INCLUDES
#include <string>
#include <sstream>
#include <iostream>
#include <vector>
#ifdef ONLY_VECTOR
#include <array>
#include <deque>
#include <list>
#include <forward_list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <stack>
#include <queue>
#endif
#endif
#ifdef TO_STRING
template <class T>
std::string toStringIterable(const T& c) {
std::ostringstream result;
result << "{ ";
for (auto &v : c) {
result << v << ", ";
}
result << " }";
return result.str();
}
#define TO_STRING_DEF(Ts, CLASS) \
template <Ts> \
std::string toString(const CLASS &c) { return toStringIterable(c); }
#define TO_STRING_DEF_T(CLASS) TO_STRING_DEF(class T, CLASS)
TO_STRING_DEF_T(std::vector<T>)
#ifdef ONLY_VECTOR
#define COMMA ,
TO_STRING_DEF(class T COMMA size_t N, std::array<T COMMA N>)
TO_STRING_DEF_T(std::deque<T>)
TO_STRING_DEF_T(std::list<T>)
TO_STRING_DEF_T(std::forward_list<T>)
TO_STRING_DEF_T(std::set<T>)
TO_STRING_DEF_T(std::multiset<T>)
TO_STRING_DEF_T(std::unordered_set<T>)
TO_STRING_DEF_T(std::unordered_multiset<T>)
TO_STRING_DEF(class K COMMA class V, std::map<K COMMA V>)
TO_STRING_DEF(class K COMMA class V, std::multimap<K COMMA V>)
TO_STRING_DEF(class K COMMA class V, std::unordered_map<K COMMA V>)
TO_STRING_DEF(class K COMMA class V, std::unordered_multimap<K COMMA V>)
TO_STRING_DEF_T(std::stack<T>)
TO_STRING_DEF_T(std::queue<T>)
TO_STRING_DEF_T(std::priority_queue<T>)
#endif
#endif
#ifdef EMPTY_MAIN
int main() {}
#else
int main() {
std::cout << toString(std::vector<int>{1,2,3,4,5}) << std::endl;
}
#endif
END
echo "Compilers used:"
for compiler in $COMPILERS; do
echo "$compiler version:"
"$compiler" --version
done
for compiler in $COMPILERS; do
echo "%%%%%%%%%"
echo "$SCENARIOS" |
while read flags; do
cmd=$(echo "$compiler" -std=c++11 -Wextra -Wall -o "$EXE" $(sed -r 's/ +/ -D/g' <<< " $flags") "$SRC")
$cmd || { echo "Failed: $cmd"; exit 1; }
spent="$({ time "$EXE"; } |& sed -n 's/real.//p')"
echo $spent "$compiler scenario: $flags"
done
done
@mat-tso
Copy link
Author

mat-tso commented Mar 9, 2016

Result on my computer:

Compilers used:
clang++ version:
clang version 3.7.1 (tags/RELEASE_371/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix
g++ version:
g++ (GCC) 5.3.0
Copyright © 2015 Free Software Foundation, Inc.
Ce logiciel est libre; voir les sources pour les conditions de copie.  Il n'y a PAS
GARANTIE; ni implicite pour le MARCHANDAGE ou pour un BUT PARTICULIER.

%%%%%%%%%
0m0.002s clang++ scenario: EMPTY_MAIN
0m0.002s clang++ scenario: INCLUDES EMPTY_MAIN ONLY_VECTOR
0m0.001s clang++ scenario: INCLUDES EMPTY_MAIN
0m0.004s clang++ scenario: INCLUDES EMPTY_MAIN ONLY_VECTOR TO_STRING
0m0.005s clang++ scenario: INCLUDES EMPTY_MAIN             TO_STRING
0m0.002s clang++ scenario: INCLUDES            ONLY_VECTOR TO_STRING
0m0.002s clang++ scenario: INCLUDES                        TO_STRING
%%%%%%%%%
0m0.001s g++ scenario: EMPTY_MAIN
0m0.002s g++ scenario: INCLUDES EMPTY_MAIN ONLY_VECTOR
0m0.001s g++ scenario: INCLUDES EMPTY_MAIN
0m0.001s g++ scenario: INCLUDES EMPTY_MAIN ONLY_VECTOR TO_STRING
0m0.001s g++ scenario: INCLUDES EMPTY_MAIN             TO_STRING
0m0.002s g++ scenario: INCLUDES            ONLY_VECTOR TO_STRING
0m0.001s g++ scenario: INCLUDES                        TO_STRING

The timing are very variable from one run to an other (1ms to 5ms independently of what was compiled).
Conclusion: the include and overload of unused containers is negligible compared to the compilation of a single empty file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment