Last active
March 9, 2016 22:21
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Result on my computer:
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.