Live data from Hacker News

Ask HN: C/C++ developer wanting to learn efficient Python

news.ycombinator.com

31–40 of 50 posts

Re: Ask HN: C/C++ developer wanting to learn efficient Python

#31
There's probably not a close analogy for Python; aside frim basic algorithmic things that transfer directly, there are some optimization gotchas in Python, but if you have real concern for optimization its about knowing the ecosystem and when to use native extensions (either existing ones or writing your own), not mostly about writing faster code in Python.

Re: Ask HN: C/C++ developer wanting to learn efficient Python

#32
I have been writing python for 15 years now and only occasionally have needed or wanted to optimize algorithms. When I have, the general takeaways have been:

* 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

#33

Advanced Python Mastery: https://news.ycombinator.com/item?id=36785005 Book: High Performance Python

I'm the co-author of High Performance Python, Micha and I are working on the 3rd ed (for 2025). Lots of bits of the book came from my past conference talks, they're available here (and the public talks will generally be on youtube): https://speakerdeck.com/ianozsvald

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)

* https://github.com/pyutils/line_profiler/

Re: Ask HN: C/C++ developer wanting to learn efficient Python

#35

Python 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…

Yeah, I need to take some forget-me-nots when I have to work on the service our system's architect said had to be in Golang, and is constantly undergoing API changes because things keep getting added/changed. It currently serves ~1000 requests per day, with a goal of ~60,000 if we increase uptake across teams. Much traffic. Wow. My last job I built and maintained a group of Python microservices that handled ~500 requests/second (load balanced across ~20 containers).

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

#36
post #23

In 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.

Ordered by insertion (see [Mailinglist](https://mail.python.org/pipermail/python-dev/2017-December/1...))

This might or might not be what you want/expect...

Re: Ask HN: C/C++ developer wanting to learn efficient Python

#37

Definitely 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.

More function calls make python slower.

Re: Ask HN: C/C++ developer wanting to learn efficient Python

#38
post #23

In 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.

I believe lower_bound is ordering by key comparison - python dicts are insertion ordered.

Re: Ask HN: C/C++ developer wanting to learn efficient Python

#39
post #23

In 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.

A little nitpick: they were always ordered by insertion, 3.7 just codified that behavior as a guarantee instead of an implementation detail.

Re: Ask HN: C/C++ developer wanting to learn efficient Python

#40
post #23

In 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.

You're right, it's even python 3.6, and you have also the OrderedDict https://realpython.com/python-ordereddict/
Post reply on HN