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
class A { | |
std::vector<std::string> _values; | |
public: | |
void addValue(const std::string &val) { | |
// some logic | |
_values.emplace_back(val); | |
} | |
}; |
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
class A { | |
int member{}; | |
public: | |
A(A&&) = default; // Explicitly tell the compiler to generate member-wise move constructor; | |
~A() {}; // User defined destructor; | |
}; |
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 <string> | |
#include <utility> | |
#include <iostream> | |
class A { | |
int member{}; | |
public: | |
~A() {}; // User defined destructor; | |
}; |
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<typename T> | |
void f(T&& param); | |
// T is deduced so && means universal reference | |
void f(std::string&& param); | |
// No deduction involved so && means r-value reference | |
template<typename T> | |
void a(T&& param) { /*does something*/ } | |
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
size_t val = 0; | |
// val expression is l-value | |
val = 1; | |
size_t fn() { // ... } | |
// Result of fn call expr is an r-value | |
fn(); | |
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
std::vector<std::string> all_strings; | |
for(int i = 0; i < aLenght; ++i) { | |
std::string result = aCallThatProducesAStr(i); | |
// Note that push_back result will copy the contents of | |
// result to an new object that will be stored in the vector. | |
// all_strings.emplace_back(result); | |
// Since is the last use of result string object, move | |
// which is more efficient. |
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
class MyBenchmark: AnyBenchmark { | |
let name: String | |
let settings: [BenchmarkSetting] | |
let closure: () throws -> Void | |
let tearDownClosure: () -> Void | |
init(_ name: String, settings: [BenchmarkSetting], | |
closure: @escaping () throws -> Void, | |
tearDownClosure: @escaping () -> Void) { | |
self.name = name |
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
typealias Vec4 = (Int8, Int8, Int8, Int8) | |
@inline(__always) | |
func vecAdd(x: inout ContiguousArray<Vec4>, s: Int8) { | |
for i in 0..<x.count { | |
x[i].0 &+= s | |
x[i].1 &+= s | |
x[i].2 &+= s | |
x[i].3 &+= s | |
} | |
} |
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
// The vectorizer can determine the best vector width, but let's consider vectorize_width(4) | |
#pragma clang loop vectorize(enable) vectorize_width(4) | |
int addScalar(int *x, int n, int s) { | |
for (int i = 0; i < n/4/*vector_width*/; ++i) | |
// The pseudo code bellow would be performed as a single SIMD vector instruction. | |
x[i] += s; | |
x[i + 1] += s; | |
x[i + 2] += s; | |
x[i + 3] += s; | |
} |
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
int addScalar(int *x, int n, int s) { | |
for (int i = 0; i < n; ++i) | |
x[i] += s; | |
} |