centralized performance resources

This commit is contained in:
Barrett Ruth 2026-01-10 12:21:52 -05:00
commit 50b15a1522
63 changed files with 328466 additions and 0 deletions

View file

@ -0,0 +1,9 @@
BasedOnStyle: Google
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortCompoundRequirementOnASingleLine: false
AllowShortEnumsOnASingleLine: false
AllowShortFunctionsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: false
AllowShortLambdasOnASingleLine: false
AllowShortLoopsOnASingleLine: false

35
perf-cpp/scripts/.clangd Normal file
View file

@ -0,0 +1,35 @@
CompileFlags:
Add:
-O2
-Wall
-Wextra
-Wpedantic
-Wshadow
-Wformat=2
-Wfloat-equal
-Wlogical-op
-Wshift-overflow=2
-Wnon-virtual-dtor
-Wold-style-cast
-Wcast-qual
-Wuseless-cast
-Wno-sign-promotion
-Wcast-align
-Wunused
-Woverloaded-virtual
-Wconversion
-Wsign-conversion
-Wmisleading-indentation
-Wduplicated-cond
-Wduplicated-branches
-Wlogical-op
-Wnull-dereference
-Wformat=2
-Wformat-overflow
-Wformat-truncation
-Wdouble-promotion
-Wundef
-DLOCAL
-Wno-unknown-pragmas
-e -std=c++23
-e -std=c++23

9
perf-cpp/scripts/a.cc Normal file
View file

@ -0,0 +1,9 @@
#include <iostream>
#include "a.hh"
int main() {
std::cout << add();
return 0;
}

3
perf-cpp/scripts/a.hh Normal file
View file

@ -0,0 +1,3 @@
#pragma once
int add() {return 1;}

View file

@ -0,0 +1,29 @@
#include <iostream>
#include <bitset>
using namespace std;
#include <bitset>
#include <iostream>
std::bitset<4> rotl(std::bitset<4> bits) {
return (bits << 1) | (bits >> 3);
}
void william() {
int will;
if (69)
int will;
}
int main()
{
std::bitset<4> bits1{ 0b0001 };
std::cout << rotl(bits1) << '\n';
std::bitset<4> bits2{ 0b1001 };
std::cout << rotl(bits2) << '\n';
return 0;
}

48
perf-cpp/scripts/ch11.cc Normal file
View file

@ -0,0 +1,48 @@
/*
Write a constexpr function template with a non-type template parameter that
returns the factorial of the template argument. The following program should
fail to compile when it reaches factorial<-3>().
*/
template <int N>
constexpr long long fibonacci() {
static_assert(N >= 0);
if constexpr (N <= 1) {
return N;
} else {
return fibonacci<N - 1>() + fibonacci<N - 2>();
}
}
// int main() {
// static_assert(fibonacci<1LL>() == 1);
// // static_assert(factorial<3>() == 6);
// // static_assert(factorial<5>() == 120);
//
// // fibonacci<-3>();
//
// return 0;
// }
#include <iostream>
template <typename T>
T add(T x, T y) {
return x + y;
}
template <typename T>
T mult(T t, int N) {
return t * N;
}
int main() {
std::cout << add(2, 3) << '\n';
std::cout << add(1.2, 3.4) << '\n';
std::cout << mult(2, 3) << '\n';
std::cout << mult(1.2, 3) << '\n';
return 0;
}

50
perf-cpp/scripts/ch15.cc Normal file
View file

@ -0,0 +1,50 @@
/*
Question #1
Write a class template named Triad that has 3 private data members with
independent type template parameters. The class should have a constructor,
access functions, and a print() member function that is defined outside the
class.
The following program should compile and run:
*/
#include <iostream>
#include <string>
template <typename T1, typename T2, typename T3>
class Triad {
private:
T1 t1;
T2 t2;
T3 t3;
public:
explicit Triad() {
}
explicit Triad(T1 t1, T2 t2, T3 t3) : t1(t1), t2(t2), t3(t3) {
}
T1 first() { return t1; }
void print() ;
~Triad() {
}
};
template <typename T1, typename T2, typename T3>
void Triad<T1, T2, T3>::print() {
std::cout << '[' << t1 << ", " << t2 << ", " << t3 << "]\n";
}
int main() {
Triad t1{1, 2, 3};
t1.print();
std::cout << '\n';
std::cout << t1.first() << '\n';
using namespace std::literals::string_literals;
const Triad t2{1, 2.3, "Hello"s};
t2.print();
std::cout << '\n';
return 0;
}

38
perf-cpp/scripts/ch4.cc Normal file
View file

@ -0,0 +1,38 @@
/*
Write the following program: The user is asked to enter 2 floating point numbers (use doubles). The user is then asked to enter one of the following mathematical symbols: +, -, *, or /. The program computes the answer on the two numbers the user entered and prints the results. If the user enters an invalid symbol, the program should print nothing.
*/
#include <iostream>
int main() {
double x, y;
std::cout << "enter x: "<<'\n';
std::cin >> x;
std::cout << "enter x: "<<'\n';
std::cin >> y;
std::cout << "enter symbol " << '\n';;
char symbol;
std::cin >> symbol;
double ans;
switch (symbol) {
case '+':
ans = x + y;
break;
case '-':
ans = x - y;
break;
case '*':
ans = x * y;
break;
case '/':
ans = x / y;
break;
default:
return 0;
}
std::cout << "answer: " << ans << '\n';
return 0;
}

51
perf-cpp/scripts/ch5.cc Normal file
View file

@ -0,0 +1,51 @@
#include <cassert>
#include <cmath>
#include <concepts>
#include <iomanip>
#include <iostream>
#include <string>
#include <string_view>
template <std::integral T>
T binexp(T base, T exponent, T mod) {
assert(mod > 0 && exponent > 0);
T product{1};
T multiplier{base % mod};
while (exponent > 0) {
if ((exponent & 1) != 0) {
product = (multiplier * product) % mod;
}
multiplier = (multiplier * multiplier) % mod;
exponent >>= 1;
}
return product;
}
int main() {
using namespace std::string_view_literals;
std::string S{"hello"};
std::string_view s{S};
S.~basic_string();
// double ub - free + string view dereferencing destroyed object
// std::cout << s << '\n' ;
std::cout << -5 / 2 << '\n';
using namespace std;
long double ld = pow((long double)10, 18); // computed in floating point
long long ll = (long long)ld; // cast back to integer
cout << fixed << setprecision(0);
cout << "pow(10,18) as long double: " << ld << "\n";
cout << "cast to long long: " << ll << "\n";
cout << "Should be exactly: " << 1000000000000000000LL << "\n";
std::cout << binexp(2, 5, 5);
return 0;
}

20
perf-cpp/scripts/ch6.cc Normal file
View file

@ -0,0 +1,20 @@
#include <iostream>
#include <cstdint>
/*
Write a program that asks the user to input an integer, and tells the user whether the number is even or odd. Write a constexpr function called isEven() that returns true if an integer passed to it is even, and false otherwise. Use the remainder operator to test whether the integer parameter is even. Make sure isEven() works with both positive and negative numbers
.*/
constexpr bool isEven(std::int32_t number) {
return number % 2 == 0;
}
int main() {
std::cout << "input an integer: \n";
std::int32_t x{};
std::cin >> x;
std::cout << std::boolalpha << isEven(x);
return 0;
}

View file

@ -0,0 +1,18 @@
#include <iostream>
int main() {
std::cout << "Enter a number: \n";
int n;
std::cin >> n;
int m;
std::cout << "Enter another number: \n";
std::cin >> m;
std::cout << "Diff: " << n - m << "; Sum: " << n + m << '\n';
return 0;
}

View file

@ -0,0 +1,2 @@
-O0
-std=c++20

View file

@ -0,0 +1,14 @@
#include <iostream>
consteval int foo(int x) {
if (x <= 1) return x;
return foo(x - 1) + foo(x - 2);
}
int main() {
constexpr int x = foo(3);
static_assert(x != foo(4));
return 0;
}

View file

@ -0,0 +1,10 @@
#include <iostream>
void f(double) { std::cout << "double\n"; }
void f(int) { std::cout << "long\n"; }
int main() {
short x = 5;
f(x); // Which one?
}

View file

@ -0,0 +1,14 @@
-g3
-fsanitize=address,undefined
-fsanitize=float-divide-by-zero
-fsanitize=float-cast-overflow
-fno-sanitize-recover=all
-fstack-protector-all
-fstack-usage
-fno-omit-frame-pointer
-fno-inline
-ffunction-sections
-D_GLIBCXX_DEBUG
-D_GLIBCXX_DEBUG_PEDANTIC
-DLOCAL
-std=c++23

View file

@ -0,0 +1,50 @@
#include <iostream>
struct Counter {
inline static int x = 1;
static void bump() { ++x; }
};
struct Counter2 : Counter {
inline static int x = 100;
static void bump() { x += 10; }
};
enum E { A = 1, B = 2 };
struct S {
enum E { B = 3, C = 4 };
static int f() { return B; }
};
enum class M : unsigned char { A = 250, B, C };
int g() {
int B = 40;
return S::f() + B;
}
int main() {
Counter::bump();
Counter2::bump();
// 112
int t1 = Counter::x + Counter2::x;
// 43
int u1 = g();
int B = 7;
// 9
int t2 = B + ::B;
int d;
{
using enum M;
// 251 - 250 = 1?
d = static_cast<int>(B) - static_cast<int>(A);
}
std::cout << t1 << " " << u1 << " " << t2 << " " << d << "\n";
}

View file

@ -0,0 +1,2 @@
1
2

View file

@ -0,0 +1,7 @@
Enter a number:
Enter another number:
Diff: -1; Sum: 3
[code]: 0
[time]: 2.52771 ms
[debug]: false

View file

View file

@ -0,0 +1,14 @@
file.cc: In function int main():
file.cc:66:23: error: narrowing conversion of 3.0e+0 from double to int [-Wnarrowing]
66 | vector<int> a= { 3.0};
| ^
file.cc: At global scope:
file.cc:39:13: warning: void YES() defined but not used [-Wunused-function]
39 | static void YES() {
| ^~~
file.cc:35:13: warning: void NO() defined but not used [-Wunused-function]
35 | static void NO() {
| ^~
cc1plus: note: unrecognized command-line option -Wno-sign-promotion may have been intended to silence earlier diagnostics
[code]: 1

32
perf-cpp/scripts/loops.cc Normal file
View file

@ -0,0 +1,32 @@
#include <iostream>
void call_for() {
for (int i = 0; i < 5; ++i) {
std::cout << (1 + 123 * 6 ^ 3 << 2);
}
}
void call_while() {
int i = 0;
while (i < 5) {
std::cout << (1 + 123 * 6 ^ 3 << 2);
++i;
}
}
void call_do_while() {
int i = 0;
do {
std::cout << (1 + 123 * 6 ^ 3 << 2);
++i;
} while (i < 5);
}
int main() {
call_for();
call_while();
call_do_while();
return 0;
}

30
perf-cpp/scripts/makefile Normal file
View file

@ -0,0 +1,30 @@
.PHONY: run debug clean setup init
VERSION ?= 20
SRC = $(word 2,$(MAKECMDGOALS))
.SILENT:
run:
sh scripts/run.sh $(SRC)
debug:
sh scripts/debug.sh $(SRC)
clean:
rm -rf build/*
setup:
test -d build || mkdir -p build
test -d io || mkdir -p io
test -d scripts || mkdir -p scripts
test -f .clang-format || cp $(HOME)/.config/cp-template/.clang-format .
test -f compile_flags.txt || cp $(HOME)/.config/cp-template/compile_flags.txt . && echo -std=c++$(VERSION) >>compile_flags.txt
test -f .clangd || cp $(HOME)/.config/cp-template/.clangd . && echo -e "\t\t-std=c++$(VERSION)" >>.clangd
init:
make setup
%:
@:

View file

@ -0,0 +1,29 @@
#!/bin/sh
. ./scripts/utils.sh
SRC="$1"
BASE=$(basename "$SRC" .cc)
INPUT="${BASE}.in"
OUTPUT="${BASE}.out"
DBG_BIN="${BASE}.debug"
test -d build || mkdir -p build
test -d io || mkdir -p io
test -f "$INPUT" && test ! -f "io/$INPUT" && mv "$INPUT" "io/"
test -f "$OUTPUT" && test ! -f "io/$OUTPUT" && mv "$OUTPUT" "io/"
test -f "io/$INPUT" || touch "io/$INPUT"
test -f "io/$OUTPUT" || touch "io/$OUTPUT"
INPUT="io/$INPUT"
OUTPUT="io/$OUTPUT"
DBG_BIN="build/$DBG_BIN"
compile_source "$SRC" "$DBG_BIN" "$OUTPUT" @debug_flags.txt
CODE=$?
test $CODE -gt 0 && exit $CODE
execute_binary "$DBG_BIN" "$INPUT" "$OUTPUT" true
exit $?

View file

@ -0,0 +1,29 @@
#!/bin/sh
. ./scripts/utils.sh
SRC="$1"
BASE=$(basename "$SRC" .cc)
INPUT="${BASE}.in"
OUTPUT="${BASE}.out"
RUN_BIN="${BASE}.run"
test -d build || mkdir -p build
test -d io || mkdir -p io
test -f "$INPUT" && test ! -f "io/$INPUT" && mv "$INPUT" "io/"
test -f "$OUTPUT" && test ! -f "io/$OUTPUT" && mv "$OUTPUT" "io/"
test -f "io/$INPUT" || touch "io/$INPUT"
test -f "io/$OUTPUT" || touch "io/$OUTPUT"
INPUT="io/$INPUT"
OUTPUT="io/$OUTPUT"
RUN_BIN="build/$RUN_BIN"
compile_source "$SRC" "$RUN_BIN" "$OUTPUT" ""
CODE=$?
test $CODE -gt 0 && exit $CODE
execute_binary "$RUN_BIN" "$INPUT" "$OUTPUT"
exit $?

View file

@ -0,0 +1,61 @@
#!/bin/sh
execute_binary() {
binary="$1"
input="$2"
output="$3"
is_debug="$4"
start=$(date '+%s.%N')
if [ -n "$is_debug" ]; then
asan="$(ldconfig -p | grep libasan.so | head -n1 | awk '{print $4}')"
LD_PRELOAD="$asan" timeout 2s ./"$binary" <"$input" >"$output" 2>&1
else
timeout 2s ./"$binary" <"$input" >"$output" 2>&1
fi
CODE=$?
end=$(date '+%s.%N')
truncate -s "$(head -n 1000 "$output" | wc -c)" "$output"
if [ $CODE -ge 124 ]; then
MSG=''
case $CODE in
124) MSG='TIMEOUT' ;;
128) MSG='SIGILL' ;;
130) MSG='SIGABRT' ;;
131) MSG='SIGBUS' ;;
136) MSG='SIGFPE' ;;
135) MSG='SIGSEGV' ;;
137) MSG='SIGPIPE' ;;
139) MSG='SIGTERM' ;;
esac
[ $CODE -ne 124 ] && sed -i '$d' "$output"
test -n "$MSG" && printf '\n[code]: %s (%s)' "$CODE" "$MSG" >>"$output"
else
printf '\n[code]: %s' "$CODE" >>"$output"
fi
printf '\n[time]: %s ms' "$(awk "BEGIN {print ($end - $start) * 1000}")" >>$output
test -n "$is_debug" && is_debug_string=true || is_debug_string=false
printf '\n[debug]: %s' "$is_debug_string" >>$output
return $CODE
}
compile_source() {
src="$1"
bin="$2"
output="$3"
flags="$4"
test -f "$bin" && rm "$bin" || true
g++ @compile_flags.txt $flags "$src" -o "$bin" 2>"$output"
CODE=$?
if [ $CODE -gt 0 ]; then
printf '\n[code]: %s' "$CODE" >>"$output"
return $CODE
else
echo '' >"$output"
return 0
fi
}

View file

@ -0,0 +1,40 @@
#include <iostream>
class S {
// void print() const { a = 3; std::cout << S::a << '\n'; }
int a = 0;
public:
S(int x) : x(x) {
}
int x{2};
};
class X {
int x;
public:
X() : x(0) {};
void get() {
std::cout << "x; " << x << '\n';
}
int& y() {
return x;
}
};
int main() {
// S{}.print();
// S.x;
// std::cout << S{3}.x << '\n';
X x;
auto& ret = x.y();
// ++ret;
x.get();
// X x;
return 0;
}

View file

@ -0,0 +1,39 @@
#include <iostream>
extern volatile int sink;
int g(int x) {
if (x == 100) sink = 42;
else if (x == 101) sink = 7;
else if (x == 102) sink = 99;
else if (x == 103) sink = 13;
else if (x == 104) sink = 5;
else if (x == 105) sink = 88;
else if (x == 106) sink = 21;
else if (x == 107) sink = 60;
else if (x == 108) sink = 33;
else if (x == 109) sink = 1;
else if (x == 110) sink = 77;
else if (x == 111) sink = 12;
else if (x == 112) sink = 54;
else if (x == 113) sink = 31;
else if (x == 114) sink = 73;
else if (x == 115) sink = 16;
else sink = -1;
return sink;
}
int main() {
char x;
std::cin >> x;
if (x == 'x') {
std::cout << "hi\n";
} else {
if (x == 'y') {
std::cout << "sup\n";
}
}
return 0;
}