4 ms·
I can see what you're saying but it's worth contemplating what percentage of the total codebase that Python code would represent if it were replaced by C++ code
by justin66 14y ago
I can see what you're saying but it's worth contemplating what percentage of the total codebase that Python code would represent if it were replaced by C++ code.
- stefantalpalaru 14y agoPoint taken, but if by this metric the percentage replaced by C++ code is still bigger my original criticism still stands.
- alpatters 14y agoI often convert python code to C++ and you'd be surprised how often the difference in loc is not that much. Certainly no hassle to do so. Not that I'm suggesting that one always should. Several boost libraries are inspired by python and C++11 features all help to write C++ code that is surprisingly similar to python, with a bit of extra type sepcification. If you think otherwise I'd suggest you're probably thinking of the C++ of the 90s - a more C with classes, and not modern C++. I often find it useful to develop and prototype in python and convert to C++. Most often I'm doing this because my python simulations can take days and the C++ versions hours. Often I find it is not just the performance critical areas, but it is easy enough just to wholesale convert the lot.
- danieldk 14y agoI often convert python code to C++ and you'd be surprised how often the difference in loc is not that much. I agree, especially with C++11 and (besides Boost) Qt. It's often the header/code separation that makes things a bit tedious, having to keep function and method signatures in-sync. Of course, if you are template-land that is not that much of a problem.
- justin66 14y agoThanks, this is interesting. I'm thinking of getting back into C++ after being forced to work on (of all things) a C project. It's hard to know where to dig in, since actually working with existing code means not playing with C++11.
- berkut 14y agoI'm working on a project right now where we're replacing a Qt interface written in Python (PyQt) with C++ Qt as it's way too slow (interface is really laggy), and other than things like having to occasionally delete objects manually (often, if you just add a widget to a layout, Qt does the deletion for you), it's pretty much a 1-1 mapping. We're also replacing core op-graph and geometry processing from Python to C++, and again it's close to 1-1 - there's a bit of overhead in the loops - in our coding style we're caching begin iterators on the line before the loop, but other than that it's very close. And the speed of the app is so much faster it's not even funny.
- AnthonyMouse 14y ago>in our coding style we're caching begin iterators on the line before the loop Can you give an example of what this looks like?
- berkut 14y agopython: for face in mesh.faces(): faceCentre = Point() for v in face.vertices(): faceCentre.add(mesh.getPoint(v)) faceCentre.div(len(face.vertices())) C++: std::vector<Face>::const_iterator itFace = mesh.getFaces().begin(); for (; itFace != mesh.getFaces().end(); ++itFace) { const Face& face = *itFace; Point faceCentre; std::vector<unsigned int>::const_iterator itVertex = face.vertices.begin(); for (; itVertex != face.vertices().end(); ++itVertex) { const unsigned int& pointIndex = *itVertex; faceCentre += mesh.getPoint(pointIndex); } faceCentre /= (float)face.vertices().size(); } So the C++ is longer, but you've got braces, and the references to the Face and unsigned int pointIndex are placed as a local variables, which makes debugging much easier - they could be inlined. It's possible to get that down even more using more modern C++ - using auto variables and not declaring the start iterator on it's own line. So yes, counting braces, it can be a lot more lines, but if you don't count braces, it's generally not that much more.
- AnthonyMouse 14y agoI guess what I'm asking is, why are you declaring the start iterator on its own line? What benefit does it have? Or does is just make the for loop line shorter? Also, as of C++11 you can do this: for(const Face& face : mesh.getFaces()) { Point faceCentre; for(size_t pointIndex : face.vertices()) faceCentre += mesh.getPoint(pointIndex); faceCentre /= static_cast<float>(face.vertices().size()); }