Live data from Hacker News

How many lines of C it takes to execute a + b in Python

codeconfessions.substack.com

111–120 of 224 posts

Re: How many lines of C it takes to execute a + b in Python

#112
post #4

A while back someone posted their patch to cpython where they replaced the hash function with a fast one and claimed this dramatically sped up the whole Python runtime. They claimed that the hash function was used constantly —e.g. 11 times in print("hello world")—because it's used to look up object properties. Apparently the default implementation is not optimized for performance but for security, just in case the so…

This may be a somewhat uninformed opinion, but I think CPython is just straight up not particularly good software. There are a million and one optimizations that other major scripting runtimes (V8, LuaJIT, PyPy, Ruby YJit etc.) have had for years that CPython is lacking. This is by design though. CPython has never been focused on performance, that's why it's not even JIT. It optimizes for simplicity and easy interope…

It's great software for the use case that Python is intended for. Python is supposed to be glue code. You embed a scripting runtime in your application, do all the heavy lifting in C, but configure your building blocks in Python so that you can easily reconfigure them as needs change.

NumPy, SciPy, TensorFlow, PyTorch, JAX, Pandas, Pillow, lxml, cjson, PyCapnP, Tornado, fast-avro, etc. all get it right. They are wrappers around C (or in some cases: Fortran/assembly/CUDA) code, where the overflow of Python method dispatch is dwarfed by the hundreds of thousands of iterations of an inner loop that's in optimized, vectorized assembly. Django, Protobufs, and Avro get it wrong (often for portability or developer velocity sake), where they wrote the whole library in Python at the expense of performance.

I was briefly tempted to write an API-compatible reimplementation of Django with the core in C++ when I left Google, but by then Django (and server-side web programming) was already falling out of favor, and if you're just shipping JSON to a SPA you can use cjson with any number of fast wsgi or asgi gateways.

Re: How many lines of C it takes to execute a + b in Python

#113

Earlier quoted context omitted.

If this is by design and it optimizes for simplicity and easy interoperability with C, why would you assess its quality as 'good software' based on criteria that are non-goals?

In my eyes, "good software" does not necessarily just mean something that accomplishes its goals. You can set a goal of being bad, and that's kind of what CPython is doing. Of course, "good" and "bad" are relative. If you don't care about performance then there's nothing wrong with CPython.

I agree with that estimate, I think it was really the idea of Python to be less than "good" by design.

I mean one doesn't need more than:

    >>> exit
    Use exit() or Ctrl-D (i.e. EOF) to exit
to see. They have that special handling, but they still don't want to let you out, because... IMO, they just want to be annoying.

When a solution could be something like:

    Note: exit() is needed in scripts. In this prompt Ctrl-D (i.e. EOF) can also be used.
    Exiting.

Re: How many lines of C it takes to execute a + b in Python

#114

Earlier quoted context omitted.

It's even easier to just buy a desk calculator or even just learn long addition and do it with pencil and paper.

Actually, I generally recommend to not leave easy tasks to the computer. The brain likes to have to do stuff sometimes.

why waste time?

Re: How many lines of C it takes to execute a + b in Python

#115
post #4

A while back someone posted their patch to cpython where they replaced the hash function with a fast one and claimed this dramatically sped up the whole Python runtime. They claimed that the hash function was used constantly —e.g. 11 times in print("hello world")—because it's used to look up object properties. Apparently the default implementation is not optimized for performance but for security, just in case the so…

> I'd much prefer to have a "I'm offline, please run twice as fast!"

If I know anything about programmers, it's that everyone would just use the "go faster" flag by default.

Re: How many lines of C it takes to execute a + b in Python

#116
The general topic is covered in Brett Cannon's “Python is (mostly) made of syntactic sugar” series of posts, via translation of high-level semantics into a subset of Python. LWN has a high-level summary: https://lwn.net/Articles/942767/

The actual posts are here: https://snarky.ca/tag/syntactic-sugar/ (multiple pages!)

Re: How many lines of C it takes to execute a + b in Python

#117

Earlier quoted context omitted.

"I would rather not read this article than have to click a button" is an interesting level of entitlement. Hope you never had your fingers stained by the ink when reading the newspaper, you probably would have sued the New York Times.

Dude we had a whole web browser revolution in the early 2000s centered largely around the ability to prevent browser pop-up windows. The fact that people are doing it in the canvas now doesn't suddenly make it not incredibly obnoxious. Pop-up windows were bad design 20 years ago and they're bad design now.

Pop-up windows that take focus over other running applications are a whole another ball game, dude. In-app popups are a pattern that may or may not be annoying, like most things used well or badly. This case seems pretty innocuous to me.

Re: How many lines of C it takes to execute a + b in Python

#118
post #113

Earlier quoted context omitted.

In my eyes, "good software" does not necessarily just mean something that accomplishes its goals. You can set a goal of being bad, and that's kind of what CPython is doing. Of course, "good" and "bad" are relative. If you don't care about performance then there's nothing wrong with CPython.

I agree with that estimate, I think it was really the idea of Python to be less than "good" by design. I mean one doesn't need more than: >>> exit Use exit() or Ctrl-D (i.e. EOF) to exit to see. They have that special handling, but they still don't want to let you out, because... IMO, they just want to be annoying. When a solution could be something like: Note: exit() is needed in scripts. In this prompt Ctrl-D (i.e.…

Yeah, that message is a pet peeve of mine. Like, you know what I'm trying to do.

Re: How many lines of C it takes to execute a + b in Python

#119
We know that addition is a relatively trivial operation, say, on 64 bit integers.

A more interesting example IMO would be something like "how many lines of C it takes to execute person.name='Bob' in Python, where person.name is undefined".

That would better demonstrate why we use Python in the first place (hint: it's not "to add integers"), while also indicating why it is slow.

Re: How many lines of C it takes to execute a + b in Python

#120

Earlier quoted context omitted.

Dude we had a whole web browser revolution in the early 2000s centered largely around the ability to prevent browser pop-up windows. The fact that people are doing it in the canvas now doesn't suddenly make it not incredibly obnoxious. Pop-up windows were bad design 20 years ago and they're bad design now.

Pop-up windows that take focus over other running applications are a whole another ball game, dude. In-app popups are a pattern that may or may not be annoying, like most things used well or badly. This case seems pretty innocuous to me.

No, they are always annoying. I'm here to read an article, the only thing this pop-up window does is get in my way.
Post reply on HN