CXX | Macros | Preprocessed Macros | Templates |
---|---|---|---|
clang 4.0 | 40s | 37s | 30s |
g++ 6.2 | 61s | 66s | 48s |
This file contains hidden or 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
template<class L, class R> | |
auto fold(L l, R r) { | |
using lTag = typename L::tag; | |
using rTag = typename R::tag; | |
if constexpr (is_base_of<rTag, BarTag>::value) { | |
if constexpr (is_same<lTag, FooTag>::value) { | |
return foldFB(l, r); | |
} else { | |
return foldBB(l, r); | |
} |
This file contains hidden or 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
template<unsigned n> | |
struct Fibonacci { | |
static const unsigned value = Fibonacci<n - 1>::value + Fibonacci<n - 2>::value; | |
}; | |
template<> | |
struct Fibonacci<0> { | |
static const unsigned value = 0; | |
}; |
This file contains hidden or 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
constexpr unsigned fibonacci(const unsigned x) { | |
return x <= 1 ? | |
1 : | |
fibonacci(x - 1) + fibonacci(x - 2); | |
} | |
int main() { | |
return fibonacci(5); | |
} |
This file contains hidden or 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
import com.google.common.base.Charsets; | |
import com.google.common.io.MoreFiles; | |
import com.google.common.io.ByteSink; | |
import java.nio.file.Path; | |
public final class GuavaFileUtils { | |
private GuavaFileUtils() { | |
} |
This file contains hidden or 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
// Create a vector of 10 elements, all initialized to 0 | |
auto v = Vector<10>(0); | |
// Set the 1st element to 7 | |
v[0] = 7; | |
// Set the 9th element to 3 | |
v[8] = 3; | |
// Print the sum of all elements |
This file contains hidden or 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
template<int D> | |
struct Vector { | |
static constexpr unsigned N = D; | |
int data[N]; | |
Vector(Vector<D> const& v) { | |
for (int i = 0; i < N; ++i) { | |
data[i] = v[i]; | |
} | |
} |
This file contains hidden or 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
#define CREATE_VECTOR(D) \ | |
struct Vector_##D {\ | |
static constexpr int N = D;\ | |
int data[N];\ | |
\ | |
Vector_##D (Vector_##D const& v) {\ | |
for(int i=0; i<N ; ++i) {\ | |
data[i] = v[i];\ | |
}\ | |
}\ |
This file contains hidden or 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
// Vector<16> is in another binary | |
extern template class Vector<16>; |
This file contains hidden or 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
#include <vector.hpp> | |
// Instantiation of common sizes | |
template class Vector<16>; | |
template class Vector<32>; | |
template class Vector<64>; | |
template class Vector<128>; |