Тёмный

Your C++ code is slow - 8 reasons why 

Zen Sepiol
Подписаться 2,3 тыс.
Просмотров 2,5 тыс.
50% 1

8 tips on how to make your C++ code run faster.
The left part of the thumbnail is created with DALL-E, an AI system by OpenAI.
Tools that I use:
www.virtualbox...
kubuntu.org/
code.visualstu...
llvm.org/
clang.org/
mesonbuild.com...
In code:
github.com/ren...
github.com/Zen...
github.com/oco...
github.com/epe...

Опубликовано:

 

16 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 26   
@aytee5862
@aytee5862 2 года назад
When I saw the thumbnail and title, for a second I asked myself "Why is MrBeast talking about C++?"
@ZenSepiol
@ZenSepiol 2 года назад
As MrBeast I would give away millions of $ to see software developers suffer through bad code ;-)
@ohwow2074
@ohwow2074 Год назад
Passing by reference is usually inefficient when it comes to types that are 16 bytes or smaller. Remember that references are not costless. Usually they are implemented using a raw pointer. So passing a reference to a function actually copies an 8-byte pointer to the called function's stack frame. But if you pass by value then only the small object itself would be copied/moved. No pointer needed. And also remember that the underlying pointer has to be dereferenced in order to get to the actual object and dereferencing is not cheap. For instance, the compiler vendors recommend that you pass `std::span` by value and avoid references because they say it's more efficient. `span` is a 16-byte type. For anything bigger than 16 bytes you need to do some benchmarks in order to reveal what strategy is the most efficient. But usually for types bigger than 16 bytes passing by reference is a safe bet. Note: The above statements apply to X86-64 architecture. They may not be true for other architectures.
@andres_sempai
@andres_sempai 2 года назад
Great video
@tristanmisja
@tristanmisja Год назад
Woah, this is a great video! I didn't know some of these features existed!
@ZenSepiol
@ZenSepiol Год назад
Glad to hear!
@az-kalaak6215
@az-kalaak6215 Год назад
Hi! Concerning the parsing of the maps, you should not even use the following code: for (auto &value : map) but for (auto &[key, value] : map) this allows you to have a clean way to iterate with the key and the value instead of one variable :)
@ZenSepiol
@ZenSepiol Год назад
Yes, 100% correct!
2 года назад
Constexpr is very usefull and its range of applications keeps growing with every new release. However as much as it seems like a straightforward tool, there are lots of cases when it doesn't work as you expect. I would suggest Jason's video for more information: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-4pKtPWcl1Go.html
@ZenSepiol
@ZenSepiol 2 года назад
Thanks for sharing!
@bennguyen1313
@bennguyen1313 6 месяцев назад
What do you normally use for writing GUI applications? I tried wxWidgets but would like to try GTK because it has an editor (Glade/Cambalache). However, not sure what I need to install.. Meson, MSYS2, GTKMM vs GTK4 vs GTK3 etc. For example, I have Visual Studio Community do I still need MSYS2 (pacman -S mingw-w64-x86_64-toolchain base-devel etc) ? or can I just unzip the GTK source and build using VS? Do I need gvsbuild (Visual Studio Project or Meson Project) ? Also for testing , do you recommend CppUTest, google test , catch2, doctest, ??
@ZenSepiol
@ZenSepiol 6 месяцев назад
For testing I use Catch2. For GUI apps either Dear ImGui or QT. I have a video for Dear Imgui which explains what packages you need.
@user-ox7gf3ok9n
@user-ox7gf3ok9n 2 года назад
This was helpful. My go to thing is early returns. Why go through the tons of conditions and nest ifs if you already know that function will do nothing in particular case. But this stuff is debatable.
@ZenSepiol
@ZenSepiol 2 года назад
Good example! A thought through early return can lead to better and faster code.
@sergeykolesnik1171
@sergeykolesnik1171 5 месяцев назад
Very misleading about constexpr. Your example will be fully evaluated at compile time with optimization enabled. And constexpr doesn't guarantee compile time evaluation, unless you use it within a const context (template argument evaluation), or declare the result variable as constexpr.
@stwe88
@stwe88 Год назад
Honestly, you should get a decent IDE or upgrade VS Code with plugins. Something like this in 2:19: float a = 0.1, shouldn't happen if you're teaching C++ and it's about the data type. Here 0.1 is a double that needs to be converted. You will only have overlooked that; hence my recommendation of a good IDE.
@ZenSepiol
@ZenSepiol Год назад
Thanks for the hint. What is your go-to IDE choice?
@stwe88
@stwe88 Год назад
@@ZenSepiol I use CLion with the SonarLint plugin under Linux. There would be a thick yellow wavy line under the 0.1 and on the right bar of the window as well. Typos are deadly speed traps in C++ (copy instead of reference). On Windows I can recommend Visual Studio with the Resharper plugin.
@ZenSepiol
@ZenSepiol Год назад
@@stwe88 I'll try the plugin, thanks!
@geoffmulberry
@geoffmulberry Год назад
Thanks for reminding me that you can pass arguments by reference. I know I learned this before, but I must have forgotten. I was passing very large std::vectors to some helper functions that I wrote by value rather than by reference and changing them to const references sped up my code about 30x. 😆 I was wondering why it was so dang slow... This is C++ after all!
@ZenSepiol
@ZenSepiol Год назад
Nice job! In C++ it's often the small things.
@zephrys.9712
@zephrys.9712 2 года назад
Thx for the new helpful video Nice to see you with a new haircut)
@ZenSepiol
@ZenSepiol 2 года назад
Thanks! 😃
@acho8387
@acho8387 Год назад
I didn't know MrBeast is a programmer too!
@АндрейМанеев-р4з
@АндрейМанеев-р4з 2 года назад
Oh, I never thought about such benefit of using auto. I will use it even more now. :) Cool video! But I think, the advice about float should not be part of it. 1. Float could be slower than double in some hardware. 2. double is default in C++ - simple mistake could make an additional casts to double for all floats around.
@ZenSepiol
@ZenSepiol 2 года назад
Thanks for the input! As always it depends on the usecase. On certain hardwares you are right. I am using float a lot to profit from SIMD vectorization and also from fewer cache misses since more values can be loaded to cache at the same time. My main point is to think about the datatype. It should be a choice and not just happen by default.
Далее
Каха и жена (недопонимание)
00:37
Real respect sig
00:48
Просмотров 1,6 млн
Premature Optimization
12:39
Просмотров 805 тыс.
C++ Super Optimization: 1000X Faster
15:33
Просмотров 319 тыс.
The Pointer to Implementation (pImpl) idiom in C++
6:54
The Dark Side of C++ - Copy-On-Write
8:39
Просмотров 2 тыс.
why are switch statements so HECKIN fast?
11:03
Просмотров 408 тыс.
How C++23 made my Thread Pool twice as fast
12:56
Просмотров 2,9 тыс.
How the C++ Compiler Works
17:55
Просмотров 805 тыс.
C++ vs Rust: which is faster?
21:15
Просмотров 394 тыс.