It's still impressive that it copied about 4GB of memory in 2 seconds
@xClairy2 жыл бұрын
69 likes Noice imma ruin it
@parlor31152 жыл бұрын
I doubt that's what happened
@themakabrapl22482 жыл бұрын
I think he is not wrong, it copied 3,7252902984619 GB of data in 2s. But I could be wrong myself I haven't made a vector with a certain size i c++ so i don't know if this is appropriate way to achive that but if it is he is not wrong
@RRKS_TF2 жыл бұрын
@@parlor3115 I am very certain that I'd did infact copy the entire vector for each call as no optimisation flags were provided meaning the assembly is functionally identical to the C++ code with no shortcuts being taken
@jvstbecause2 жыл бұрын
@@parlor3115 1 Construction + 100 Copy Constructions = 101 Reservations 101 Reservations * 10.000.000 Elements * 4 (sizeof int) / (1024 * 1024) ~= 3853 Mebibyte of Data that must be reserved. That makes almost 4 Gebibyte
@ohwow20742 жыл бұрын
And let's not forget that you didn't specify any optimizations for C++ compiler. C++ without -O2 is actually very inefficient and garbage. The whole purpose of compiled languages is the aggressive optimization possibilities.
@Vili694202 жыл бұрын
i was about to point that out as well
@kyrneh76292 жыл бұрын
If you compile it with optimization, even the first c++ code runs in exactly 0 time (at least with clang). The generated assembly is just one no-op, because the compiler realized that the whole program does nothing at all. The real takeaway of this example should thus be: "Use compiler optimizations!" Of cause "Always pass by const reference!" is also good advice to every beginner c++ programmer.
@ohwow20742 жыл бұрын
@@kyrneh7629 you can add a volatile keyword so that the compiler doesn't optimize that away. Also pass by reference is not the most efficient way for all data types. For objects that are the size of the machine word size (e.g. 8 bytes for 64 bit architectures) pass by value is simply more efficient. And for huge things like containers you either move them or pass them by reference.
@idanyel15872 жыл бұрын
Come on, man, if you add the -O2 will look ahead and it will know that the function calls are not used, so they will be eliminated. Don't mess up things if you don't know the behavior.
@maxaafbackname55622 жыл бұрын
I think that even for simple data types like integers it does not harm to pass by _const_ reference with an optimizing compiler. The reference is optimized away.into a pass by value.
@gengarboi87452 жыл бұрын
Yeah in my computer science class the importance of references was one of the first things we learned the moment we started working with std::strings and std::vectors. It may seem like a reason to argue that C++ has a higher learning curve but, at least on the surface, it seems roughly similar to Rust's borrowing system.
@redcrafterlppa3032 жыл бұрын
Don't confuse rusts strict borrowing rules with the sloppy c++ refrences. Rust is memory safe c++ is not.
@gengarboi87452 жыл бұрын
@@redcrafterlppa303 I don't see how they're any different in just this scenario. You don’t want to copy the whole object so you just "borrow" or "reference" it.
@redcrafterlppa3032 жыл бұрын
@@gengarboi8745.. And this is where the similarities end. Also where c++ refrences end. They are simply "the same" as some other variable. Rusts borrowing rules ensure valid objects in time and space. C++ has no concept of temporal bounds and limited control over spacial bounds. Rust has no null or nullptr every variable is bound to valid memory. This is what makes rust great and c++ frustrating.
@jakubskop732 жыл бұрын
@@redcrafterlppa303 C++ has fewer limitations in that respect - which is good or bad depending on your expertise
@gengarboi87452 жыл бұрын
@@redcrafterlppa303 OK well thank you for going on this massive tangent that was unrelated to my point that I don't care about. You're really doing God's work for Rust here.
@pharoah3272 жыл бұрын
As soon as you showed the C++ code, I was shouting to make it a ref (actually it should be a const ref since you aren't changing it). Any competent C++ developer should know this.
@v01d_r34l1ty2 жыл бұрын
I prefer pointers
@pharoah3272 жыл бұрын
@@v01d_r34l1ty that's fine too. For this case, I think the difference would be minimal and wouldn't have an effect.
@TheBuilder2 жыл бұрын
If you look closely my linter suggested making the vector const which is why the variable changed colour
@pharoah3272 жыл бұрын
@@TheBuilder I didn't catch that. I love static analyzers.
@dimi144 Жыл бұрын
@@v01d_r34l1ty where you can use a reference instead of a pointer, you should, this is C++, not C
@dmytrokyrychuk70492 жыл бұрын
I am a python developer, and I know very little about C++, but I would assume that the majority of C++ developers are aware of what a reference is, and are concious about which variables are passed by reference, and which are passed by value.
@stijndcl2 жыл бұрын
And if not, using a proper linter/IDE will tell you to pass by reference if you aren't. CLion for example automatically refactors this for you as well.
@brinza8882 жыл бұрын
Well, there are no auto decisions about passing by value or reference in C++ (at the end of video u can see way to fix problem and enforce compiler to pass by reference). It is responsibility of programmer, that's why it is so important. And that is why the trouble shown may appear in your code.
@dimi144 Жыл бұрын
In C++, everything is passed by value unless you specify otherwise. I also thought that C++ developers knew what a reference was, but since videos like these are so common maybe I was wrong for thinking that
@azmah1999 Жыл бұрын
@@dimi144 Nah, this type of video are common because they're trivial to make and the possible audience of people starting to learn how to code is bigger than the one of people knowing how to code
@TheBuilder Жыл бұрын
We are on KZbin
@623-x7b2 жыл бұрын
Yes this is a very important thing to be aware of as a C++ programmer. Me and a friend found out that if you are using a set (say set st) you should use st.lower_bound(value) as opposed to std::lower_bound(st.begin(), st.end(), value). The latter is linear whereas the former is logarithmic. I'm guess it also applies to upper_bound as well.
@carlgeorgbiermann29152 жыл бұрын
Just last friday, I was puzzled over the bad performance of my code, where I dealt with objects tens of megabytes large. It was exactly that: missed a & in the argument list of a function. I was really glad it was nothing more serious than that :D
@feschber2 жыл бұрын
How to make CPP slower than Python: Write the most stupid code you possibly can
@TheBuilder2 жыл бұрын
Its not that hard to write sloppy code in C++
@SunPodder2 жыл бұрын
Even by passing without reference took me 0.018s. Just use compiler optimizations :)
@PKua0072 жыл бұрын
In this case the whole loop will be optimized out, because it does nothing, unless you for example print the result. In the end all that that code will do is allocate the memory, memset it to 0, free it and return.
@TheTuubster2 жыл бұрын
There are still two "mistakes" in this code: 1. Only make it a non-const call-by-reference, if it is clear the object is going to be changed by the receiver. If not, make it a const-reference, so the one calling it knows, his object will not be changed but its status is the same after the routine ran. 2. If the routine expects to run through a list of objects that could be as large as 10 million, use an iterator and not a vector, as the iterator allows to catch the resource WHILE someone walks through the amount of objects as the vector forces to catch the complete set before passing it to another routine.
@TheBuilder2 жыл бұрын
You're right but this is above the skill level I'm targeting for this video
@TheA-rl4zo2 жыл бұрын
Could you elaborate on the second point, thank you
@nuelzemudio8832 жыл бұрын
@@TheA-rl4zo Every collection type has a dedicated iterator for traversing its collection. It is best to use the iterator to access the elements in the collection than trying to do it manually using a loop. Take for example a string. A string is just an array/collection of char objects. But the proper way to traverse a string is using string_view.
@danial_amini Жыл бұрын
@@nuelzemudio883 would it be too much to ask for your version of the c++ code? I still don't get it lol sorry (I'm a noob). So the 10 million vector is an inefficient container? Are you suggesting something else like arrays?
@sparky173j2 жыл бұрын
It's funny how much you have to do to make it slower than python. A billion int transfers is a lot!
@MoolsDog2005 Жыл бұрын
Another thing you can do with strings is that instead of passing a const reference, it’d be better to use a string_view.
@TheBuilder Жыл бұрын
yes if you plan on doing operations on that view later, passing either works
@joss85582 ай бұрын
as a former c++ programmer, I spotted the mistake in the 0.3 seconds that I saw the code in the intro
@ukaosim2 жыл бұрын
The reason that’s the case is that python passes arguments by reference 😅… when the same technique (pass by reference) is used in c++ it’s even faster 🎉
@__aot__2 жыл бұрын
Exactly. C++ always wins.
@StevenRotelli Жыл бұрын
Wow, so it was close, making a massive memory management mistake like that.
@vrtex172 жыл бұрын
Immediately when he showed a function that takes in a collection i was like "you silly goof, you're gonna pass out by value in c++"
@vrtex172 жыл бұрын
I was meant to write "pass it by reference" but I'm not correcting it.
@misaalanshori2 жыл бұрын
Yeah i haven't done this before, but that is definitely a mistake I would've made.
@tonik25582 жыл бұрын
I like writing Rust because the equivalent code would be a hard compiler error. You are passing ownership of the list to the function you call, so either pass it by reference, or explicitly call clone on it to call it in a loop. Of course the compiler tells you what the error is, why it's an error and what you can do to fix the error. A friendly compiler can help you feel like a genius.
@TheBuilder2 жыл бұрын
I have to agree, argument passing is one place C++ has artificial complexity. I think there is a new language being proposed cppfront that tries to redesign away some of the bad default behaviour
@InconspicuousChap Жыл бұрын
Anything is slower in crooked hands. Python's strength is that everything it uses is already implemented in C libraries. Python just forwards the calls. And if something is not there, you should get it implemented in a C library before you could go ahead.
@TheBuilder Жыл бұрын
correct, which makes python in the truest sense a scripting language like bash
@InconspicuousChap Жыл бұрын
@@TheBuilderit is one. It's designed for non-programmers: data scientists, sysadmins, etc. I once tried to implement a Python's math's factorial algorithm in Python itself. Got an awfully slow deficient implementation, even slower than brute force multiplication of numbers. This language is just a thin layer upon C libraries.
@kelali2 жыл бұрын
0:33 no because you are copying the vector every get_first call
@432v012 жыл бұрын
As one started programming with C++, I still prefer the C++ setting: value is value and reference is reference. The consistency makes me feel good.
@griffinschreiber6867 Жыл бұрын
I wrote an entire neural network WITHOUT USING THIS OPTIMIZATION!?????? I'm so glad I watched this video.
@wgolyoko2 жыл бұрын
If you're programming in C++ at all, you alrrady know what a reference is...
@TheBypasser9 ай бұрын
Wonder what happens should we get rid of that std_vector and go for a calloc()/free() instead...
@petrjara75592 жыл бұрын
Now make python to full copy the set 100 times
@TheBuilder2 жыл бұрын
just let python have this one ☺
@birtantaskn776211 ай бұрын
This is not actually a mistake; one can identify it as a mistake if they are unfamiliar with pointers.
@esben1812 жыл бұрын
It wouldnt surprise me because you're copying the entire array instead of passing a reference to it which is probably what Python does by default
@williamdrum98992 жыл бұрын
I'm used to assembly so I use a pointer for anything bigger than the machine's register size out of habit.
@EgnachHelton2 жыл бұрын
What's happening here isn't just copying. The call by value with std::vector ask the memory allocator to allocate memory on heap every time the function is invoked. And after each call, the cloned vector get dropped which involves a call to memory allocator to deallocate the memory. This process is much much slower than just copying megabytes of integers. Heap memory usage is much more expensive in non-GC languages like C, C++ or Rust. The fundamental reason that these languages are considered "faster" than Python or Java is because they provide tools enabling you to be precise and frugal about heap memory usage. If you were to use the "heap pointer soup" approach, which is common in Java or Python, in these languages, you are not likely to get any performance benefits.
@xeridea Жыл бұрын
I would agree, except, the reason Python is slow, is that it is an interpreted language, which in in of itself makes it about 20x slower. All that flexibility and ease of use comes at a cost.
@adrianprayoga3352 жыл бұрын
I won't take "Umm, actually you can use pointer for that" as a joke ever again
@danial_amini Жыл бұрын
very interesting. Is g++ faster or llvm (for this example)?
@TheBuilder Жыл бұрын
i doubt there would be a difference
@alexandrubossro2 жыл бұрын
That's actually a very good and useful tutorial! Thanks 🙏
@vv1zard3x2 жыл бұрын
Omg.... You code: get_first(vector c){...} Must be: get_first(const vector& c) {...} Default arguments in Python - references!))
@idanyel15872 жыл бұрын
Try to use the same thing, but add .copy() to the python list, it will be even worse The fact that c++ handles that pretty well is amazing, python is trash.
@TheBuilder2 жыл бұрын
Don't be so harsh with Python, its still better than Bash in my book
@g.42792 жыл бұрын
Performance isn't very important for most applications honestly.
@idanyel15872 жыл бұрын
@Mala Suerte, I can play the same game with you: try to develop an operating system using python. Your point is useless.
@idanyel15872 жыл бұрын
@@g.4279, right, right. Just for your knowledge, the python interpreter is written in C, data base engines are written is C / C++, mostly alll applications rely on a software written in C / C++ and you come here to say the performance isn't important, interesting.
@g.42792 жыл бұрын
@@idanyel1587 I know what Python is written in and I never said performance is *never* important. But for the majority of modern computing it isn't. The majority of software out there aren't programming languages or operating systems. Developer time is far more valuable than a few more milliseconds in most cases. Which is why you see C++ in certain industries and Python is others.
@6754bettkitty Жыл бұрын
0:21 PASS BY REFERENCE! 😱
@JokeryEU2 жыл бұрын
but you havent showed us the result AFTER the & was applied :(
@TheBuilder2 жыл бұрын
It may not be clear but when I run the two command at the end of the video, they print out the timings
@misobarisic40132 жыл бұрын
At least specify an optimisation level for c++
@TheBuilder2 жыл бұрын
I want the compiler to show you my mistakes not hide them
@islamhamdane2 жыл бұрын
wow I'm rally injoy watching your videos can you gives a carrer shift for some one whow start learning c++ in 2022,It's really help for motivation .thank's and keep going .
@dbtalaviya2 жыл бұрын
Esi galti Kickstart mai kyu nahi dikhti
@kevincole28432 жыл бұрын
important and often overlooked.
@ItsCOMMANDer_ Жыл бұрын
well written c/cpp code will always be faster than well written python code
@zipur33642 жыл бұрын
So how does python solve this issue? Automatic pointers?
@sledgex92 жыл бұрын
IIRC python's collections don't make deep copies when assigned to another variable or passed as a parameter. The new variable/parameter just points to the same object as the initial variable.
@taragnor2 жыл бұрын
Basically. It's the same way most high level languages do. There's no hidden copies. Unless you use .copy() or .clone() or whatever, you'll pass the original object. I find the hidden copies that C++ can do sometimes to be really tricky. Rust solves this problem by having move by default and no non-explicit copies.
@sledgex92 жыл бұрын
@@taragnor Personally I find the reverse true. When I assign a variable to "something" I expect it to make a copy of the data. When I mutate that "something" via the new variable I don't expect it to have sideffects on the original variable too.
@taragnor2 жыл бұрын
@@sledgex9 I mean, working with any language other than C/C++, if you take any kind of object and say a=b, the idea is that a isn't a copy of b, but rather a and b are the same object. When you want to copy something, it's an explicit action. This is important because copies can be shallow or deep, so it's a good idea to always specify when a copy is happening and what kind of copy it is. Not to mention if you've got copies of large objects going on, that can be a serious performance drag, so it's generally not a good idea to have implicit copies, except for very small data structures, like primitives, tuples of primitives, etc. With a language as focused on performance as C++ is, having it set up where you can so easily make performance crushing copies is just not very good design. The thing about C++ is that sometimes you don't get punished for this, because the compiler is pretty smart in that it will do move elision if it can to save you the copy, but in cases like this video, where it can't, it just quietly makes thousands of copies and you may be none the wiser. In Rusts case, doing a double reference to the same object would break its ownership model, so it instead has an assignment perform a move. While this can be unintuitive, the nice thing is that the compiler will warn you when you're using a variable that's already been moved, so in practice it will never actually cause you any problems. Since Rust tracks all that ownership stuff, you'll never get a case where you corrupt an existing object you still want. Basically what happens is that either you never use the original variable again, and Rust just ignores it. Or you try to use it again and get a compiler error. And the Rust method still gives you the fine control over memory that C++ would, where you know what's on the stack and what's on the heap.
@sledgex92 жыл бұрын
@@taragnor I don't know man. To me, it is far more intuitive that an assignment equals copy. To me, an assignment basically says "hey, I want my variable to have this value". If I want a mutation on my new variable to affect another variable I prefer to be explicit about that(value assigned by reference/pointer). But since this a matter of preference, there's no point in arguing about this.
@abludungeonmaster58172 жыл бұрын
Yea. Pass by reference vs pointers vs new object is a rookie mistake. Gotta understand your scope.
@brucea98712 жыл бұрын
That loud dinging sound your video made every time you pointed out a part of your program with a red arrow was very annoying.
@iamgly2 жыл бұрын
Also remember to add optimisations flag
@newuser1322 жыл бұрын
C++ being slower than Python is just a myth--in reality, they are both excellent programming languages that each have their own strengths and weaknesses. It is true that Python is generally considered more high-level than C++, so it might be easier to learn and use in certain cases, but when it comes to pure coding speed and potential, they are actually pretty close to each other. So if you're looking to be as efficient as possible in your coding, you would be well-served to learn both C++ and Python, learning each language's best practices and using them together to build more powerful and efficient applications!
@TheBuilder Жыл бұрын
Idk, the forced indents in python and lack of low level logic structures like for loops makes python hard to use for educational purposes like writing sorts for example
@xeridea Жыл бұрын
Anyone claiming Python can be as fast as C++ is delirious.
@xeridea Жыл бұрын
@@TheBuilder Another person who hates forced indents! I have intentionally not learned Python much in part for this reason. If I need to quickly write something where speed isn't important, I use PHP.
@ayoubelmhamdi79202 жыл бұрын
the time is less than one second of tree experience, that need more intention to understand how it's different, should the results be more clearly, so i think we should use 1 billions instead of 1 million
@tashishsoni90112 жыл бұрын
Dayum. That's alot of diff
@sparant12 жыл бұрын
Watch how KZbinr obtains basic knowledge how computers work (pass by reference vs pass by value - always know which ur language is doing)
@TheBuilder2 жыл бұрын
Glad you enjoyed it :)
@stephenhowe41072 жыл бұрын
Yes, worked this out at 0:25
@rageworx776 ай бұрын
Understand what is optimize before compare with c++.
@TheBuilder6 ай бұрын
👍
@awabkhan29772 жыл бұрын
Good video.
@TheBuilder2 жыл бұрын
Glad you enjoyed it
@vedqiibyol2 жыл бұрын
Or just don't use the standard library......
@TheBuilder2 жыл бұрын
I can't live without my vectors
@surv5k2 жыл бұрын
@@TheBuilder vectors are life
@mochou_p2 жыл бұрын
and use an array if you care about speed
@IchiganCS2 жыл бұрын
Aren't const ref vectors just more elaborate arrays? I think accessing a vector and accessing an array is actually the same operation, isn't it?
@TheBuilder2 жыл бұрын
@@IchiganCS The underlying data of a vector is just an array. If your vector never changes size you won't notice a difference in speed. You might consider using std:array in the case you know your array will never change size
@VladykaVladykov2 жыл бұрын
Для первого класса, либо для питонистов
@weirddan45511 ай бұрын
Yes, C++ is slower than Python if you make run 10 million miles further (pass by value) after shooting it in the knee (compiled with optimizations disabled).
@TheBuilder11 ай бұрын
Yes
@dinobotpwnz2 жыл бұрын
Another reason why C is better than C++. Passing a type vs passing a pointer to that type don't just look different in the function prototype. They look different in the call as well so you get more of the benefits of static typing.
@xeridea Жыл бұрын
Both languages have their strengths. I wouldn't say either is better than the other. In C, it is much easier to have memory leaks, due to the lack of smart pointers, and being forced to manually implement many things that C++ gives you for free. C++ isn't great for kernels, drivers, or low power embedded systems though.
@alexengineering37542 жыл бұрын
A programmer that dosent know the difference between values and refferences dont deserve to Programm in c/c++
@simongido5652 жыл бұрын
What is the point of this video?
@TheBuilder2 жыл бұрын
yes
@yusufklc78212 жыл бұрын
C++ is slower when you do not now anything yess
@TheBuilder Жыл бұрын
I've seen newbies write exponential growth functions to calculate linear time problem like finding if a value exists in an array
@Awwe126752 жыл бұрын
Python is not language it’s script of c language
@anlcangulkaya62442 жыл бұрын
if you don't know putting this& you are not a programmer
@R00kTruth Жыл бұрын
my python runs much faster then the avg and im on old old gen.. but that is my secret ;-)
@GooogleGoglee2 жыл бұрын
In JAVA?
@GooogleGoglee2 жыл бұрын
@patrick w r u? & Y?
@gengarboi87452 жыл бұрын
Java and C# use references in a different way. It tries to handle memory management on its own and use references automatically for large objects passed to functions. You have to use the "new" keyword to explicitly create new objects.
@sayven2 жыл бұрын
@@GooogleGoglee Primitives (i.e. those data types that start with a lower case letter, int, char, boolean...) always get copied. Non-primitives are always passed by reference (unless you specifically create a new object with a copy constructor or do something similar)
@GooogleGoglee2 жыл бұрын
❤ JAVA
@b-penajohneric1519 ай бұрын
I referencing the exact code to Java (I'm using ArrayList as basis of flexible array), the compiler took 0 seconds (maybe milliseconds) to build and run so yeah there's no need to pass down the reference as c++ does unless one of your data types is in primitive state.
@bloody17872 жыл бұрын
Lol, what kind of nonsense did I look at? The author is clearly not strong in C++
@TheBuilder2 жыл бұрын
why is that
@n00blamer2 жыл бұрын
Someone missed the title of the video.. :_D
@pattappat2 жыл бұрын
hello c++ users - I am the average python user. this is definitely not a mistake, it will make your code faster. yes definitely. this is not an attempt to sabotage the c++ community and become one rank higher in speed than before, no no most certainly not. please carry on making this mistake- I mean discovery.
@protonray2 жыл бұрын
C++ is not for beginners
@TheBuilder2 жыл бұрын
first language i learned in school was C
@protonray2 жыл бұрын
@@TheBuilder As a rule of thumb for c++ beginners: pass integral values per value and everything else per const ref. If const ref will not fit for a certain case - then you have to adapt it with care.
@n00blamer2 жыл бұрын
@@protonray System V AMD64 ABI is pretty neat.. Integer/Pointer Arguments 1-6 RDI, RSI, RDX, RCX, R8, R9 Floating Point Arguments 1-8 XMM0 - XMM7 Excess Arguments Stack If you pass float or double as reference, you take performance hit. If you pass __mm128* by reference, you get hit again. The MS 64 bit ABI is a bit less stellar but same general rules of thumb apply.
@protonray2 жыл бұрын
@@n00blamer maybe it was confusing, with integral values I mean (bool, u/ints, floats/doubles) and aliases on them.
@yeeter2692 жыл бұрын
Nice
@_Admin2 жыл бұрын
bruh
@VanStabHolme Жыл бұрын
This was underwhelming
@ragilmalik2 жыл бұрын
Pypy is actually much muuuuch faster than cpython. Still like thrice slower than c but 100x faster than cpython.
@ChronicoOne2 жыл бұрын
Key takeaway: pass big data by reference so you don't have to copy it all every time it is used. Also, good to use compiler optimizations unless you think you're better at assembly than the compiler. I know I'm not🫠
@oaw_manofnorse2 жыл бұрын
Ignorant Python programmers! C++ release programs must be compiled with -O3 or -O4, and then the C++ result is > time ./a.out real 0m00.00s user 0m00.00s sys 0m00.00s