Ask HN: C/C++ developer wanting to learn efficient Python
31–40 of 50 posts
Re: Ask HN: C/C++ developer wanting to learn efficient Python
#32* Flattening data from hierarchical, custom types into flatter, builtin collection types can make a speed difference. The builtin type methods spend more time in the native code and are optimized.
* Lots of things that I thought could make a difference but would barely move the needle. There is so much pointer indirection internal to CPython.
* The set and frozenset types can offer easy and immediate algorithmic improvements. This is obvious from a formal/academic perspective but continues to be my favorite thing about writing quick and dirty python. I have encountered cases where an algorithm called for a fancy data structure but doing simple set intersection and difference was good enough.
* Over time, Python taught me to worry less about optimizing things that didn't matter. Everyone pays lip service to the perils of premature optimization but I think there are layers to the problem. Fast languages make many affordances for optimization, and to some extent compel you to make detailed choices that have performance implications (e.g. choice of integer width, choice of collection type). There is something very liberating about reminding myself "it's going to be slow either way, and it doesn't matter." When I work in Swift for example I find myself getting distracted by finer details that relates to efficiency.
Re: Ask HN: C/C++ developer wanting to learn efficient Python
#33Advanced Python Mastery: https://news.ycombinator.com/item?id=36785005 Book: High Performance Python
Mostly that content has a scientific focus but the obvious thing that carries over to any part of Python is _profiling_ to figure out what's slow. Top tools I'd recommend are:
* https://pypi.org/project/scalene/ combined cpu+memory+gpu profiling
* https://github.com/gaogaotiantian/viztracer get a timeline of execution vs call-stack (great to discover what's happening deep inside pandas)
* my https://pypi.org/project/ipython-memory-usage/ if you're in Jupyter Notebooks (built on https://github.com/pythonprofilers/memory_profiler which sadly is unmaintained)
Re: Ask HN: C/C++ developer wanting to learn efficient Python
#34Re: Ask HN: C/C++ developer wanting to learn efficient Python
#35Python is a very fast language, but not in the sense that you would expect as a C++ developer: its execution is (comparatively) very slow, but it shines at the speed of development. Many things that one might take as given in other languages, in Python are optional : static type analysis, multithreading, immutability and the like. When it comes to writing algorithms in Python, it's best to think about it as executabl…
I have no doubt that Golang is faster and more efficient for HTTP serving, but if you don't have a good specification for your app you're happy with for at least the next however long and need to keep moving crap around, I'd so much prefer Python.
And if I hear some flavor of "compile-time checks are tests/but Python doesn't check types" argument, that person shouldn't be involved in software development.
Re: Ask HN: C/C++ developer wanting to learn efficient Python
#36In Python job interviews, I think the interviewer will only judge your code on asymptotic complexity, not absolute speed. I think Python engineers generally aren't expected to know how to micro-optimize their Python code. Some general tips for algorithmic complexity in Python: - Python's list is equivalent to C++ std::vector. If you need to push/pop at the head of the list, use Python's "collections.deque" to avoid t…
"- Python's dict is a fast unordered hashmap. However, if you need order-aware operations like C++'s std::map::lower_bound(), you're out of luck; Python's standard library doesn't have a tree implementation." I'm fairly certain that in Python 3.7 and later standard library dictionaries are now ordered by default.
This might or might not be what you want/expect...
Re: Ask HN: C/C++ developer wanting to learn efficient Python
#37Definitely recommend readint PEP-8 to anyone period. It forces you to write smaller helper methods and make your code more concise, similar in vein to CleanCode principles. Theres all sorts of write ups on writing performant Python, it just comes depending on what you are working on, chances are high you are writing algos that have already been optimized and prewritten for you.
Re: Ask HN: C/C++ developer wanting to learn efficient Python
#38In Python job interviews, I think the interviewer will only judge your code on asymptotic complexity, not absolute speed. I think Python engineers generally aren't expected to know how to micro-optimize their Python code. Some general tips for algorithmic complexity in Python: - Python's list is equivalent to C++ std::vector. If you need to push/pop at the head of the list, use Python's "collections.deque" to avoid t…
"- Python's dict is a fast unordered hashmap. However, if you need order-aware operations like C++'s std::map::lower_bound(), you're out of luck; Python's standard library doesn't have a tree implementation." I'm fairly certain that in Python 3.7 and later standard library dictionaries are now ordered by default.
Re: Ask HN: C/C++ developer wanting to learn efficient Python
#39In Python job interviews, I think the interviewer will only judge your code on asymptotic complexity, not absolute speed. I think Python engineers generally aren't expected to know how to micro-optimize their Python code. Some general tips for algorithmic complexity in Python: - Python's list is equivalent to C++ std::vector. If you need to push/pop at the head of the list, use Python's "collections.deque" to avoid t…
"- Python's dict is a fast unordered hashmap. However, if you need order-aware operations like C++'s std::map::lower_bound(), you're out of luck; Python's standard library doesn't have a tree implementation." I'm fairly certain that in Python 3.7 and later standard library dictionaries are now ordered by default.
Re: Ask HN: C/C++ developer wanting to learn efficient Python
#40In Python job interviews, I think the interviewer will only judge your code on asymptotic complexity, not absolute speed. I think Python engineers generally aren't expected to know how to micro-optimize their Python code. Some general tips for algorithmic complexity in Python: - Python's list is equivalent to C++ std::vector. If you need to push/pop at the head of the list, use Python's "collections.deque" to avoid t…
"- Python's dict is a fast unordered hashmap. However, if you need order-aware operations like C++'s std::map::lower_bound(), you're out of luck; Python's standard library doesn't have a tree implementation." I'm fairly certain that in Python 3.7 and later standard library dictionaries are now ordered by default.