Skip to content

Instantly share code, notes, and snippets.

View LucianoPAlmeida's full-sized avatar
💭
Daniel 11:32

Luciano Almeida LucianoPAlmeida

💭
Daniel 11:32
View GitHub Profile
class A {
std::vector<std::string> _values;
public:
void addValue(const std::string &val) {
// some logic
_values.emplace_back(val);
}
};
class A {
int member{};
public:
A(A&&) = default; // Explicitly tell the compiler to generate member-wise move constructor;
~A() {}; // User defined destructor;
};
#include <string>
#include <utility>
#include <iostream>
class A {
int member{};
public:
~A() {}; // User defined destructor;
};
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*/ }
size_t val = 0;
// val expression is l-value
val = 1;
size_t fn() { // ... }
// Result of fn call expr is an r-value
fn();
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.
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
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
}
}
// 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;
}
int addScalar(int *x, int n, int s) {
for (int i = 0; i < n; ++i)
x[i] += s;
}