this post was submitted on 29 Aug 2024
916 points (97.9% liked)

Programmer Humor

32503 readers
644 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] raspberriesareyummy@lemmy.world 9 points 2 months ago (2 children)

So, basically, use it like you would use Python.

That's a great way to get performance as shitty as python's.

[–] zik@lemmy.world 41 points 2 months ago (3 children)

C++ can do a lot of things but one thing it can't do is perform as poorly as python.

[–] raspberriesareyummy@lemmy.world 4 points 2 months ago

Not quite, but smart pointers in the wrong location can be quite wasteful in terms of CPU cycles.

[–] thebestaquaman@lemmy.world 15 points 2 months ago (1 children)

This is a very "yes but still no" thing in my experience. Typically, I find that if I write "naive" C++ code, where I make no effort to optimise anything, I'll outperform python code that I've spent time optimising by a factor of 10-30 (given that the code is reasonably complex, this obviously isn't true for a simple matrix-multiplication where you can use numpy). If I spend some time on optimisation, I'll typically be outperforming python by a factor of 50+.

In the end, I've found it's mostly about what kind of data structures you're working with, and how you're passing them around. If you're primarily working with arrays of some sort and doing simple math with them, using some numpy and scipy magic can get you speeds that will beat naive C++ code. On the other hand, when you have custom data structures that you want to avoid unnecessarily copying, just rewriting the exact same code in C++ and passing things by reference can give you massive speedups.

When I choose C++ over python, it's not only because of speed. It's also because I want a more explicitly typed language (which is easier to maintain), overloaded functions, and to actually know the memory layout of what I'm working with to some degree.

[–] raspberriesareyummy@lemmy.world 5 points 2 months ago

I guess I should have clarified in my original comment that I was exaggerating - obviously, C++ doesn't get as bad as python, not even into the same ballpark.

My emphasis was on "don't use C++ like you would python" because that's not good advice imo.