3 ms·
> a truly equivalent program would have to offer all the same run-time dynamic type manipulation and nearly arbitrarily modifiable object instances involved. I
by dkersten 8y ago
> a truly equivalent program would have to offer all the same run-time dynamic type manipulation and nearly arbitrarily modifiable object instances involved.
In actual production code, I rarely need this though. I mean, sure, for framework code, but in C++ I can do quite a lot with the standard library and templates. Sure it’s not so runtime flexible as Python, but for normal production code, I’ve rarely found it necessary. Don’t get me wrong, I absolutely prefer Python for most of the use cases that it’s conmonly used for, but I don’t agree quite agree that the quoted text above matters for most uses.
> What Python really proposes is this: “I’ll give you 100x more flexibility in exchange for 40x worse performance for certain classes of operations.”
Have you used a recent version of C++? I think the 100x might have been true 15 years ago, but, while Python has a lot, lot more runtime flexibility, modern C++ is a pretty productive environment, without sacrificing performance.
I’m not arguing that Python isn’t more flexible, especially at runtime, or that it’s not more productive. Of course it is. But you’re paying a performance premium for it and I don’t think the productivity gap is as large as the performance gap anymore.
With all that said, I do agree with your overall message. Python gives you productivity and flexibility and it’s important to focus on that rather than performance and, as you say, Cython gives you options when you do need performance. But the difference between Python and C++ nowadays, for normal production code, isn’t that huge anymore in my opinion.
Of course, there are plenty of reasons to prefer Python though: fantastic web frameworks and batteries included libraries, fantastic community, fantastic documentation to name a few.
- mlthoughts2018 8y agoI won’t be able to address all the points. But in my experience, C++ has been going in the wrong direction in terms of simplicity or productivity. Modern C++ is drastically more complicated and riddled with deep esoterica. The flexibility benefit modern Python gives is probably more like 1000x, further mitigating many cases when there are concerns about the flexibility/performance trade-off. Just for one example about what I mean with modern C++: < https://bitbashing.io/std-visit.html https://bitbashing.io/std-visit.html >. This example is relevant because you mentioned you feel you never need Python’s flexibility in the day to day. But in Python (much like in many functional languages), the entire idea of the visitor pattern can be boiled down to map() —- just one function call! If you want to specialize this for your own data type, you can very easily implement the iterator protocol, often simple __iter__, __getitem__, and __next__, which themselves could use e.g. numba to iterate some homogeneously typed tree class with pure C performance characteristics, or a sum type (like with Cython fused types) as in the std::visit example, etc. The point is that the data model’s flexibility makes this trivial, rather than convoluted. In the cases when I’d care about this pattern, it would be no contest that Python offers hugely improved productivity. So it’s not just naive flexibilty of modifying object instances, but the bigger picture of flexibility in the data model overall.
- dkersten 8y ago> The flexibility benefit modern Python gives is probably more like 1000x In my personal experience, this hasn’t been true for me. Granted, I’ve been mostly ignoring C++17 because I like to give compilers a little time to catch up, so I haven’t been exposed to std::visit beyond reading about it, but C++11 (and 14 to a lesser extent) have been really productive for me. It’s more verbose for sure, but that’s largely a non-issue especially with editor support. I also find it easier to manage and localise state in C++ (I’m a Clojure developer at heart so carefully management of mutable state is important to me) simply because in Python everything (including method dispatch) is mutable and in large codebasrs it can be very difficult to know when things are being changed behind my back. C++ allows you to mutate things too, of course, and since you have low level memory access you can do some truly horrific things, but most of the time it doesn’t happen outside of isolated performance optimisations so it hasn’t been an issue for me. TL;DR: Python is great and productive, but I personally am really productive in C++ too. EDIT: you edited while I was writing, so in response to the latter part of your message: std::visit, at least from my understanding of it, is really rather misnamed. It’s not a generic visitor pattern that you might apply over a collection but a tool for applying different functions on a variant, based on the type contained in the variant. Your example of Python visitor pattern boiling down to map() and a few dunder functions isn’t really different from C++ where it boils down to iterators. You have a lot of the flexibility available at compile time with templates too, my comment about not needing the flexibility was directed specifically at runtime modifying of objects (messing with its dispatch dict), stuff like overriding dunder methods is akin to templates in C++ in many ways and that flexibility exists in both languages. Btw to be clear, I’m not saying it’s not easier in Python, because it obviously is easier, I’m just saying that for me the productivity gap isn’t thaaaat large for most code, I don’t need the runtime flexibility in most code and C++ doesn’t pay a performance premium. I should add that I’m nitpicking a bit and not suggesting people use C++ over Python at all. I agree with a lot of what you’re saying, my disagreement is really over the productivity gap, since if you truly need the performance (most use cases don’t), then going directly to C++, at least for me, is often a sensible choice.
- stochastic_monk 8y agoIt primarily depends on the size of the project. Past a certain size, I am definitely more productive in C++ than in Python. Though the compile-time checking of Cython might balance things out and make scaling a Python project easier. std::visit is broken, sure. std::vector<bool> is a mistake, too. You don't have to use it. Select a subset of the language which gives you what you need. That's part of what's great about having so much in the language.
- fjsolwmv 8y ago> I don’t think the productivity gap is as large as the performance gap anymore. It's impossible to make a general statement like this. Every project has a different performance budget.
- dkersten 8y agoSure it’s a bit over-general. Having said that, when I write bad C++ code t still typically runs faster than decent Python code, for typical use cases. I also don’t find the productivity loss of using C++, as a language (I mean, I’d still be more productive using django than a c++ web framework, just because django is super comprehensive), isn’t all that great. Also, when I need performance in C++, I have a lot more options than in Python and even on core data structures I have options (eg in Python I have a list, in c++ I have linked lists, vectors, deques, destructive versions of these, arrays, trees... memory pooled versions, cache line aligned versions, parallel versions etc). Don’t get me wrong, for many use cases where Python is often used, like web development, I’d much rather use and be more productive in Python, but I disagree with the premise that Python is so much more productive than C++ that the performance overhead is negligible. Even if in most cases the cost is worth it. It’s worth noting a counter to my argument though and that is for data analysis, Python is largely just glue between libraries that are implemented in C, so the performance difference here is largely negligible.