Reputation: 9373
As a university project I wrote a rudimentary benchmark for various sorting algorithms. I have to use several compiler flags such as -Wall -g0 -O3
and C++14 standard.
At some point I realized that a few time measurements didn't make sense and algorithm optimizations had no effect. A fellow student ran the same code on a Linux machine with GCC and it worked as expected. Hence, it must have been (and still is) a LLVM/clang++ configuration issue – at least that's my assumption.
So I checked the build settings within my IDE (Xcode 7.1.1) and even compiled the sources on the shell, but the problem remains.
Finally, I had a look at Xcode's report navigator and used the verbose output (-v
) on the terminal. It revealed that clang++ is using many other parameters per default:
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.11.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -disable-free -disable-llvm-verifier -main-file-name Benchmark.cpp -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 253.6 -v -dwarf-column-info -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/7.0.0 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk -stdlib=libc++ -O3 -Wall -std=c++14 -fdeprecated-macro -fdebug-compilation-dir /Users/Kruse/Downloads/ProgramOptimization -ferror-limit 19 -fmessage-length 142 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime=macosx-10.11.0 -fencode-extended-block-signature -fcxx-exceptions -fexceptions -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -vectorize-loops -vectorize-slp -o /var/folders/q4/rj3wqzms2fdcqlxgdzb3l5hc0000gn/T/Benchmark-1912ed.o -x c++ Benchmark.cpp
How can I strip this down to the crucial parts? Or am I on the wrong track?
Here's the code under test (don't blame me for the bubblesort-like swapping – it's a requirement):
class InsertionSort {
public:
template <typename T, size_t SIZE>
static void sort(std::array<T, SIZE> &field) {
for (size_t global = 1; global < SIZE; global++) {
for (size_t sorted = global; sorted > 0
&& field[sorted] < field[sorted - 1]; sorted--) {
std::swap(field[sorted], field[sorted - 1]);
}
}
}
template <typename T, size_t SIZE>
static void sortGuard(std::array<T, SIZE> &field) {
size_t minIndex = MinimumSearch::getMin(field);
std::swap(field[minIndex], field[0]);
for (size_t global = 2; global < SIZE; global++) {
for (size_t sorted = global; field[sorted] < field[sorted - 1]; sorted--) {
std::swap(field[sorted], field[sorted - 1]);
}
}
}
};
Minimum search is implemented as follows:
class MinimumSearch {
public:
template <typename T, size_t SIZE>
static size_t getMin(std::array<T, SIZE> &field, size_t startIndex = 0) {
size_t minIndex = startIndex;
T minVal = field[minIndex];
for (size_t i = minIndex; i < SIZE; i++) {
if (field[i] < minVal) {
minIndex = i;
minVal = field[i];
}
}
return minIndex;
}
};
InsertionSort#sortGuard(std::array<T, SIZE>&, size_t)
should be faster than the default sort method. That's the case using GCC, but not with LLVM/clang++.
Upvotes: 0
Views: 1053
Reputation: 218750
If you don't have command line tools installed, you can install them with the terminal command:
xcode-select --install
Once installed, you can run clang
from the command line with something like:
clang++ -std=c++14 -O3 test.cpp
Upvotes: 1