C++ can be slower than Python?

แชร์
ฝัง
  • เผยแพร่เมื่อ 11 พ.ย. 2022
  • This C++ program is MUCH slower compared to Python because of this programming Mistake
    Speed comparison between a Python program and a C++ program.
    ==== Awesome Links ====
    🗣 Discord: / discord
    ✍️ Github: github.com/TheBuilder-software
    💎 Patreon: / topshelftechnology
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 203

  • @cipherxen2
    @cipherxen2 ปีที่แล้ว +926

    It's still impressive that it copied about 4GB of memory in 2 seconds

    • @xClairy
      @xClairy ปีที่แล้ว +3

      69 likes Noice imma ruin it

    • @parlor3115
      @parlor3115 ปีที่แล้ว +5

      I doubt that's what happened

    • @themakabrapl2248
      @themakabrapl2248 ปีที่แล้ว +35

      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_TF
      @RRKS_TF ปีที่แล้ว +11

      @@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

    • @jvstbecause
      @jvstbecause ปีที่แล้ว +14

      @@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

  • @ohwow2074
    @ohwow2074 ปีที่แล้ว +378

    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.

    • @Vili69420
      @Vili69420 ปีที่แล้ว +5

      i was about to point that out as well

    • @kyrneh7629
      @kyrneh7629 ปีที่แล้ว +57

      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.

    • @ohwow2074
      @ohwow2074 ปีที่แล้ว +5

      @@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.

    • @idanyel1587
      @idanyel1587 ปีที่แล้ว +14

      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.

    • @maxaafbackname5562
      @maxaafbackname5562 ปีที่แล้ว +2

      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.

  • @gengarboi8745
    @gengarboi8745 ปีที่แล้ว +125

    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.

    • @redcrafterlppa303
      @redcrafterlppa303 ปีที่แล้ว +19

      Don't confuse rusts strict borrowing rules with the sloppy c++ refrences. Rust is memory safe c++ is not.

    • @gengarboi8745
      @gengarboi8745 ปีที่แล้ว +17

      @@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.

    • @redcrafterlppa303
      @redcrafterlppa303 ปีที่แล้ว +8

      @@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.

    • @jakubskop73
      @jakubskop73 ปีที่แล้ว +14

      @@redcrafterlppa303 C++ has fewer limitations in that respect - which is good or bad depending on your expertise

    • @gengarboi8745
      @gengarboi8745 ปีที่แล้ว +7

      @@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.

  • @dmytrokyrychuk7049
    @dmytrokyrychuk7049 ปีที่แล้ว +53

    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.

    • @stijndcl
      @stijndcl ปีที่แล้ว +3

      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.

    • @brinza888
      @brinza888 ปีที่แล้ว +2

      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
      @dimi144 6 หลายเดือนก่อน

      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
      @azmah1999 5 หลายเดือนก่อน +1

      @@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
      @TheBuilder  5 หลายเดือนก่อน +1

      We are on TH-cam

  • @pharoah327
    @pharoah327 ปีที่แล้ว +71

    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_r34l1ty
      @v01d_r34l1ty ปีที่แล้ว +4

      I prefer pointers

    • @pharoah327
      @pharoah327 ปีที่แล้ว +2

      @@v01d_r34l1ty that's fine too. For this case, I think the difference would be minimal and wouldn't have an effect.

    • @TheBuilder
      @TheBuilder  ปีที่แล้ว +18

      If you look closely my linter suggested making the vector const which is why the variable changed colour

    • @pharoah327
      @pharoah327 ปีที่แล้ว +2

      @@TheBuilder I didn't catch that. I love static analyzers.

    • @dimi144
      @dimi144 6 หลายเดือนก่อน

      ​@@v01d_r34l1ty where you can use a reference instead of a pointer, you should, this is C++, not C

  • @623-x7b
    @623-x7b ปีที่แล้ว +15

    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.

  • @carlgeorgbiermann2915
    @carlgeorgbiermann2915 ปีที่แล้ว +6

    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

  • @sparky173j
    @sparky173j ปีที่แล้ว +13

    It's funny how much you have to do to make it slower than python. A billion int transfers is a lot!

  • @TheBypasser
    @TheBypasser 3 หลายเดือนก่อน

    Wonder what happens should we get rid of that std_vector and go for a calloc()/free() instead...

  • @SunPodder
    @SunPodder ปีที่แล้ว +21

    Even by passing without reference took me 0.018s. Just use compiler optimizations :)

    • @PKua007
      @PKua007 ปีที่แล้ว +5

      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.

  • @alexandrubossro
    @alexandrubossro ปีที่แล้ว +7

    That's actually a very good and useful tutorial! Thanks 🙏

  • @danial_amini
    @danial_amini ปีที่แล้ว

    very interesting. Is g++ faster or llvm (for this example)?

    • @TheBuilder
      @TheBuilder  ปีที่แล้ว

      i doubt there would be a difference

  • @misaalanshori
    @misaalanshori ปีที่แล้ว +4

    Yeah i haven't done this before, but that is definitely a mistake I would've made.

  • @JokeryEU
    @JokeryEU ปีที่แล้ว

    but you havent showed us the result AFTER the & was applied :(

    • @TheBuilder
      @TheBuilder  ปีที่แล้ว

      It may not be clear but when I run the two command at the end of the video, they print out the timings

  • @TheTuubster
    @TheTuubster ปีที่แล้ว +3

    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.

    • @TheBuilder
      @TheBuilder  ปีที่แล้ว +1

      You're right but this is above the skill level I'm targeting for this video

    • @TheA-rl4zo
      @TheA-rl4zo ปีที่แล้ว

      Could you elaborate on the second point, thank you

    • @nuelzemudio883
      @nuelzemudio883 ปีที่แล้ว

      @@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
      @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?

  • @ukaosim
    @ukaosim ปีที่แล้ว +2

    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__
      @__aot__ ปีที่แล้ว +1

      Exactly. C++ always wins.

  • @islamhamdane
    @islamhamdane ปีที่แล้ว

    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 .

  • @MoolsDogTwoOfficial
    @MoolsDogTwoOfficial 8 หลายเดือนก่อน +1

    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
      @TheBuilder  8 หลายเดือนก่อน +2

      yes if you plan on doing operations on that view later, passing either works

  • @kevincole2843
    @kevincole2843 ปีที่แล้ว

    important and often overlooked.

  • @tonik2558
    @tonik2558 ปีที่แล้ว

    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.

    • @TheBuilder
      @TheBuilder  ปีที่แล้ว +1

      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

  • @awabkhan2977
    @awabkhan2977 ปีที่แล้ว +2

    Good video.

    • @TheBuilder
      @TheBuilder  ปีที่แล้ว +1

      Glad you enjoyed it

  • @williamdrum9899
    @williamdrum9899 ปีที่แล้ว

    I'm used to assembly so I use a pointer for anything bigger than the machine's register size out of habit.

  • @vrtex17
    @vrtex17 ปีที่แล้ว +1

    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++"

    • @vrtex17
      @vrtex17 ปีที่แล้ว

      I was meant to write "pass it by reference" but I'm not correcting it.

  • @StevenRotelli
    @StevenRotelli 9 หลายเดือนก่อน

    Wow, so it was close, making a massive memory management mistake like that.

  • @zipur3364
    @zipur3364 ปีที่แล้ว

    So how does python solve this issue? Automatic pointers?

    • @sledgex9
      @sledgex9 ปีที่แล้ว +1

      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.

    • @taragnor
      @taragnor ปีที่แล้ว

      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.

    • @sledgex9
      @sledgex9 ปีที่แล้ว +1

      @@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.

    • @taragnor
      @taragnor ปีที่แล้ว

      ​@@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.

    • @sledgex9
      @sledgex9 ปีที่แล้ว

      @@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.

  • @adrianprayoga335
    @adrianprayoga335 ปีที่แล้ว

    I won't take "Umm, actually you can use pointer for that" as a joke ever again

  • @432v01
    @432v01 ปีที่แล้ว

    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.

  • @misobarisic4013
    @misobarisic4013 ปีที่แล้ว +5

    At least specify an optimisation level for c++

    • @TheBuilder
      @TheBuilder  ปีที่แล้ว

      I want the compiler to show you my mistakes not hide them

  • @kelali
    @kelali ปีที่แล้ว +1

    0:33 no because you are copying the vector every get_first call

  • @dbtalaviya
    @dbtalaviya ปีที่แล้ว

    Esi galti Kickstart mai kyu nahi dikhti

  • @EgnachHelton
    @EgnachHelton ปีที่แล้ว +5

    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
      @xeridea 6 หลายเดือนก่อน

      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.

  • @abludungeonmaster5817
    @abludungeonmaster5817 ปีที่แล้ว

    Yea. Pass by reference vs pointers vs new object is a rookie mistake. Gotta understand your scope.

  • @6754bettkitty
    @6754bettkitty 6 หลายเดือนก่อน

    0:21 PASS BY REFERENCE! 😱

  • @esben181
    @esben181 ปีที่แล้ว +1

    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

  • @idanyel1587
    @idanyel1587 ปีที่แล้ว +3

    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.

    • @TheBuilder
      @TheBuilder  ปีที่แล้ว

      Don't be so harsh with Python, its still better than Bash in my book

    • @g.4279
      @g.4279 ปีที่แล้ว +2

      Performance isn't very important for most applications honestly.

    • @idanyel1587
      @idanyel1587 ปีที่แล้ว

      @Mala Suerte, I can play the same game with you: try to develop an operating system using python. Your point is useless.

    • @idanyel1587
      @idanyel1587 ปีที่แล้ว +1

      @@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.4279
      @g.4279 ปีที่แล้ว

      ​@@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.

  • @petrjara7559
    @petrjara7559 ปีที่แล้ว +1

    Now make python to full copy the set 100 times

    • @TheBuilder
      @TheBuilder  ปีที่แล้ว +1

      just let python have this one ☺

  • @ayoubelmhamdi7920
    @ayoubelmhamdi7920 ปีที่แล้ว

    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

  • @InconspicuousChap
    @InconspicuousChap 8 หลายเดือนก่อน +1

    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
      @TheBuilder  8 หลายเดือนก่อน

      correct, which makes python in the truest sense a scripting language like bash

    • @InconspicuousChap
      @InconspicuousChap 8 หลายเดือนก่อน

      @@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.

  • @tashishsoni9011
    @tashishsoni9011 ปีที่แล้ว

    Dayum. That's alot of diff

  • @iamgly
    @iamgly ปีที่แล้ว

    Also remember to add optimisations flag

  • @vv1zard3x
    @vv1zard3x ปีที่แล้ว

    Omg....
    You code:
    get_first(vector c){...}
    Must be:
    get_first(const vector& c) {...}
    Default arguments in Python - references!))

  • @yeeter269
    @yeeter269 ปีที่แล้ว

    Nice

  • @griffinschreiber6867
    @griffinschreiber6867 5 หลายเดือนก่อน

    I wrote an entire neural network WITHOUT USING THIS OPTIMIZATION!??????
    I'm so glad I watched this video.

  • @stephenhowe4107
    @stephenhowe4107 ปีที่แล้ว

    Yes, worked this out at 0:25

  • @wgolyoko
    @wgolyoko ปีที่แล้ว +1

    If you're programming in C++ at all, you alrrady know what a reference is...

  • @brucea9871
    @brucea9871 ปีที่แล้ว

    That loud dinging sound your video made every time you pointed out a part of your program with a red arrow was very annoying.

  • @birtantaskn7762
    @birtantaskn7762 5 หลายเดือนก่อน

    This is not actually a mistake; one can identify it as a mistake if they are unfamiliar with pointers.

  • @feschber
    @feschber ปีที่แล้ว

    How to make CPP slower than Python:
    Write the most stupid code you possibly can

    • @TheBuilder
      @TheBuilder  ปีที่แล้ว

      Its not that hard to write sloppy code in C++

  • @mochou-p
    @mochou-p ปีที่แล้ว +4

    and use an array if you care about speed

    • @IchiganCS
      @IchiganCS ปีที่แล้ว

      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?

    • @TheBuilder
      @TheBuilder  ปีที่แล้ว +1

      ​@@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

  • @vedqiibyol
    @vedqiibyol ปีที่แล้ว +1

    Or just don't use the standard library......

    • @TheBuilder
      @TheBuilder  ปีที่แล้ว

      I can't live without my vectors

    • @surv5k
      @surv5k ปีที่แล้ว

      @@TheBuilder vectors are life

  • @ItsCOMMANDer_
    @ItsCOMMANDer_ 6 หลายเดือนก่อน

    well written c/cpp code will always be faster than well written python code

  • @QuickTechNow
    @QuickTechNow 2 หลายเดือนก่อน

    Lets not forget that Python runs ontop of C++. And "-O3" would help too. Ill never understand how python scripters can call themselves programmer / code too.

  • @VladykaVladykov
    @VladykaVladykov ปีที่แล้ว +1

    Для первого класса, либо для питонистов

  • @sparant1
    @sparant1 ปีที่แล้ว

    Watch how TH-camr obtains basic knowledge how computers work (pass by reference vs pass by value - always know which ur language is doing)

    • @TheBuilder
      @TheBuilder  ปีที่แล้ว

      Glad you enjoyed it :)

  • @GooogleGoglee
    @GooogleGoglee ปีที่แล้ว

    In JAVA?

    • @GooogleGoglee
      @GooogleGoglee ปีที่แล้ว

      @patrick w r u? & Y?

    • @gengarboi8745
      @gengarboi8745 ปีที่แล้ว

      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.

    • @sayven
      @sayven ปีที่แล้ว +2

      @@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)

    • @GooogleGoglee
      @GooogleGoglee ปีที่แล้ว +1

      ❤ JAVA

    • @b-penajohneric151
      @b-penajohneric151 3 หลายเดือนก่อน

      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.

  • @simongido565
    @simongido565 ปีที่แล้ว

    What is the point of this video?

  • @_Admin
    @_Admin ปีที่แล้ว

    bruh

  • @newuser132
    @newuser132 ปีที่แล้ว

    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
      @TheBuilder  ปีที่แล้ว +1

      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
      @xeridea 6 หลายเดือนก่อน

      Anyone claiming Python can be as fast as C++ is delirious.

    • @xeridea
      @xeridea 6 หลายเดือนก่อน

      @@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.

  • @weirddan455
    @weirddan455 4 หลายเดือนก่อน

    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).

  • @Awwe12675
    @Awwe12675 ปีที่แล้ว

    Python is not language it’s script of c language

  • @yusufklc7821
    @yusufklc7821 ปีที่แล้ว

    C++ is slower when you do not now anything yess

    • @TheBuilder
      @TheBuilder  ปีที่แล้ว

      I've seen newbies write exponential growth functions to calculate linear time problem like finding if a value exists in an array

  • @anlcangulkaya6244
    @anlcangulkaya6244 ปีที่แล้ว

    if you don't know putting this& you are not a programmer

  • @R00kTruth
    @R00kTruth 7 หลายเดือนก่อน

    my python runs much faster then the avg and im on old old gen.. but that is my secret ;-)

  • @alexengineering3754
    @alexengineering3754 ปีที่แล้ว

    A programmer that dosent know the difference between values and refferences dont deserve to Programm in c/c++

  • @dinobotpwnz
    @dinobotpwnz ปีที่แล้ว

    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
      @xeridea 6 หลายเดือนก่อน

      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.

  • @VanStabHolme
    @VanStabHolme 7 หลายเดือนก่อน

    This was underwhelming

  • @bloody1787
    @bloody1787 ปีที่แล้ว

    Lol, what kind of nonsense did I look at? The author is clearly not strong in C++

    • @TheBuilder
      @TheBuilder  ปีที่แล้ว

      why is that

    • @n00blamer
      @n00blamer ปีที่แล้ว

      Someone missed the title of the video.. :_D

  • @protonray
    @protonray ปีที่แล้ว

    C++ is not for beginners

    • @TheBuilder
      @TheBuilder  ปีที่แล้ว +1

      first language i learned in school was C

    • @protonray
      @protonray ปีที่แล้ว

      @@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.

    • @n00blamer
      @n00blamer ปีที่แล้ว

      @@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.

    • @protonray
      @protonray ปีที่แล้ว

      @@n00blamer maybe it was confusing, with integral values I mean (bool, u/ints, floats/doubles) and aliases on them.

  • @ragilmalik
    @ragilmalik ปีที่แล้ว

    Pypy is actually much muuuuch faster than cpython. Still like thrice slower than c but 100x faster than cpython.

  • @pattappat
    @pattappat ปีที่แล้ว +4

    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.

  • @ChronicoOne
    @ChronicoOne ปีที่แล้ว

    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🫠

  • @manofnorse
    @manofnorse ปีที่แล้ว +2

    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