3 ms·
It's basically 100% true. Any program written in python can be translated to an equivalently functioning C++ program which requires fewer expensive operations
by dekhn 11y ago
It's basically 100% true. Any program written in python can be translated to an equivalently functioning C++ program which requires fewer expensive operations to run.
The Python VM is doing a fair bit more work per instruction it executes; for example, incrementing an int isn't going to be a simple register add, it's going to involve a pointer dereference, some comparisons and branches, execution of several C functions, and overhead for interpreting the VM bytecode.
That said, if you understand what Python is doing under the hood you can greatly speed up your programs and approach C++ performance in some areas.
That said, I strongly recommend all performance-sensitive code is written in C++ with wrappers to drive it from Python.
- ericfrederich 11y agoIt's true, you can translate any Python program to C++. When you do this you'll have the benefit of having debugged it and getting the logic correct in a safe language. Just have to make sure all of that translates correctly. It's nicer to develop an algorithm in Python first where your off by one errors will not segfault or worse silently fail.