r/Python • u/Narthal • May 02 '20
Discussion My experience learning Python as a c++ developer
First off, Python is absolutely insane, not in a bad way, mind you, but it's just crazy to me. It's amazing and kind of confusing, but crazy none the less.
Recently I had to integrate Python as a scripting language into a large c++ project and though I should get to know the language first. And let me tell you, it's simply magical.
"I can add properties to classes dynamically? And delete them?" "Functions don't even care about the number of arguments?" "Need to do something? There's a library for that."
It's absolutely crazy. And I love it. I have to be honest, the most amazing about this is how easy it is to embed.
I could give Python the project's memory allocator and the interpreter immediately uses the main memory pool of the project. I could redirect the interpreter's stdout / stderr channels to the project as well. Extending the language and exposing c++ functions are a breeze.
Python essentially supercharges c++.
Now, I'm not going to change my preference of c/c++ any time soon, but I just had to make a post about how nicely Python works as a scripting language in a c++ project. Cheers
12
u/Deezl-Vegas May 03 '20
This is kind of true, but if you look at Jonathan Blow's talks about his compiler and various other talks, you'll see that actually you can get a 200x speedup by making use of data structures that are L1 cache-optimized, which is rarely negligible. Python can almost never do that because each Python object is a wrapped pointer, there's a lot things happening like namespace lookups, and there's a lot of dicts and hash tables floating around (which are generally sort of randomly heap-allocated) and classes (which aren't stored tightly in memory like C structs usually), and the garbage collector has to run periodically, kicking things out of cache.
The PSF has done a great job with the compiler, but the structure of scripting languages themselves clashes directly with what Intel's trying to do to speed up code on the chip.