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

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;
}