How to make your STRINGS FASTER in C++!

The Cherno · 134K views · 2019 · 15:02

Open on YouTube

C, C++, Rust, Go & systems languagesSoftware performance & optimizationtutorial

Why Stack Overflow recommends it

Stack Overflow users link it as the source of the allocation-counting example code they are running and asking about when their results differ from the video's.

Recommended by 0 people across 0 different months, 2019 to 2023.

What it is

A 15-minute tutorial from The Cherno's C++ series on reducing hidden string costs. It shows how to overload operator new to count heap allocations, then covers std::string small string optimisation and using std::string_view to avoid copies.

What people said

Cites user34299 · 2019-11 · Stack Overflow · on “Memory allocation for std::string of various lengths”

#include <iostream> #include <string> static uint32_t s_AllocCount = 0; // Overload of new operator. void* operator new(size_t size) { s_AllocCount++; std::cout << "Allocating " << size << " bytes\n"; return malloc(size); } void PrintName(const std::string& name) { std::cout << name << " - " << name.length() << " characters." << std::endl; } int main() { std::string name = "123456789012345"; PrintName(name); std::cout << s_AllocCount << " allocations" << std::endl; std::cin.get(); } The above code is a slightly modified version of that in How to make your STRINGS FASTER in C++! [this video] . I am puzzled about the various outputs from this program in Visual Studio 2019 compiled in x86 debug. Whe

Cites Bulent Gercek · 2020-07 · Stack Overflow · on “error C2679: binary '<<': no operator found which takes a right-hand operand of type 'std::string_view' (or there is no acceptable conversion)”

@cigien I am using **ISO C++17 Standard (/std:c++17)**. Btw I added `#include ` and now it works. Thank you very much! But I still curious why it is not working. in Cherno's youtube video his code was working without adding `#include `. You can check from his video. link [this video]

Cites KcFnMi · 2023-12 · Stack Overflow · on “Debugging on M1 Mac and Windows is different?”

I used two environments: (MBP) 2015 MacBookPro running Windows 10 on Boot Camp with Visual Studio Community 2022 (MBA) M1 MacBookAir running macOS Ventura with QtCreator And I got different results while following [this video] and trying to debug: #include <iostream> using namespace std; static size_t s_AllocCount = 0; void* operator new(size_t size) { s_AllocCount++; // BREAKPOINT AT THIS LINE cout << "Allocating " << size << " bytes" << endl; return malloc(size); } void print(const string& s) { cout << s << endl; } int main() { cout << "starting..." << endl; string s = "bla"; print(s); } On MPB the debugger stops at the breakpoint, as in the video. But on MBA it does not stop. Why? The followin