Live data from Hacker News

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

codeconfessions.substack.com

101–110 of 224 posts

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

#101
post #61

[flagged]

Well people don’t pay and websites have bills to pay. Sure many of them want to put out good content but they need to pay the bills to keep doing that. It really is a perfect usecase for crypto currency if they could manage the throughput. They should be an option to pay a penny/fraction of a penny to view a page without ads that people could just one button click and move on. Any amount larger than a $1 and it should bring up a verification screen so people don’t get swindled.

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

#102
post #61

[flagged]

Talking about annoying ... I open a HN discussion about an article, and the first comment talks about popups, colours, fonts, cookies, ... and it triggers a long discussion. Same story again and again ... ... now, that's annoying.

Flag and move on:

> Eschew flamebait. Avoid generic tangents. Omit internet tropes.

> Please don't post shallow dismissals, especially of other people's work. A good critical comment teaches us something.

> Please don't pick the most provocative thing in an article or post to complain about in the thread. Find something interesting to respond to instead.

> Please don't complain about tangential annoyances—e.g. article or website formats, name collisions, or back-button breakage. They're too common to be interesting.

If we're consistent enough about this, eventually the community will get the message.

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

#103
post #59

Earlier quoted context omitted.

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…

The last couple of CPython versions have had dramatic speed improvements, so that demonstrates your point but also gives some hope that things are changing on that front.

Dramatic? Barely. Even less than half of the non-dramatic speed improvements they promised. The promise was like "5 times faster in the next 4 releases", and the improvements in 2 releases thus far was like 20% at best (so not even 2x faster).

Compared to JS pre-and-after modern engines it's a tiny improvement.

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

#104

Earlier quoted context omitted.

Rust is doing something even more subtle here than just making the hash function a container property. There are two inter-related Rust traits, Hash is a trait which types implement to explain abstractly how to hash that type, but it's written in terms of a Hasher trait so you can drop in a different function with the same API. The Rust standard library provides a derive macro for Hash, so most people can just gestur…

We can get into the weeds about the details, but what I'm talking about is mostly that in Python (and in most languages really, AFAIK Ruby, Java, or C# are the same to list just a few) objects have to return the hash itself, so the hash function is fixed by the type. In Rust, the Hash trait is only used to feed data to a hasher such that the type can decide what should be hashed. The creation of the hasher is done by…

The derive macro is also key here. Have you ever looked at the machinery needed for Java's URI type to have a halfway useful hashCode implementation? It's pretty elaborate, there's just no way the average programmer will do that work correctly for their own types even ignoring the desire to allow different hash functions. Rust programmers are almost always able to just write #[derive(Hash)] and not worry about it.

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

#105
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…

What's more, you can get a significant speedup from your Python scripts by replacing the inbuilt cpython malloc calls with a static "allocate big chunk of stack at the beginning and I'll manage it myself" implementation, falling back to malloc as needed if it grows beyond that. A college class in perf engineering I TA'd did this, the results even a beginner could achieve were compelling, the top of the class produced results quite remarkable indeed..

This is most effective for reducing startup time of short lived scripts, where the runtime is dominated by many thousands of trivial mallocs right at startup. But in general if you can establish a bound on memory, it will be faster to allocate it in one shot.

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

#106

Earlier quoted context omitted.

Talking about annoying ... I open a HN discussion about an article, and the first comment talks about popups, colours, fonts, cookies, ... and it triggers a long discussion. Same story again and again ... ... now, that's annoying.

Flag and move on: > Eschew flamebait. Avoid generic tangents. Omit internet tropes. > Please don't post shallow dismissals, especially of other people's work. A good critical comment teaches us something. > Please don't pick the most provocative thing in an article or post to complain about in the thread. Find something interesting to respond to instead. > Please don't complain about tangential annoyances—e.g. articl…

TIL that one can flag comments. I always wondered why there wasn't a flag link on comments.

> Click on its timestamp to go to its page, then click the 'flag' link at the top.

The extra click is probably why it isn't used much. I thought I simply didn't have enough karma.

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

#107

Earlier quoted context omitted.

It's not about what, it's about when though — like the GP, I got a pop-up fairly immediately before I'd engaged in the article so I just closed the tab. The best time to engage me is after I've enjoyed the article and hopefully interested to hear more from the writer rather than immediately landing on the page. It's the equivalent to sales staff jumping on customers in shops the minute they walk in the door — "can I…

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

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

#108

Python's math operations are going to need a lot of C code because the numbers can be any size. It's part of what makes it so great for scientific computing (as you don't have to spend hours implementing arbitrary precision math - probably badly.) If that's too slow though there's always NumPy.

If only there were libraries for multiprecison arithmetic in other languages...

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

#109

Earlier quoted context omitted.

The semantic issues of making a performant Python language implementation are more or less exactly the same as for JS and Lua, optimizing Ruby seems to possibly have even more "magic" that needs patching but we've seen the Shopify team get cracking on that (it includes MaximeCB that did HiggsJS). PyPy is in many aspects to be rated as a research project that tried a novel approach to reduce the workload compared to t…

Unfortunately Python's innard is much more complicated than most expectations. You have named JS and Lua, but those languages never have "magic" methods---JS instead has prototypes and more recently proxies, while Lua has metatables. Ordinary objects aren't magic in this sense, and conversely magical objects are generally deliberate choices in those languages. But Python's magic `__dunder__` methods are everywhere in…

Also, you have craziness like quite regular iteration being implemented using exceptions, which are not exactly trivial to optimize.

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

#110

Earlier quoted context omitted.

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…

I don't disagree with you but, now you can retrofit a JIT into python. https://blog.pyston.org/2022/09/29/announcing-3-7-3-10-suppo...

"We think of the breakdown roughly as follows: of our roughly 30% original speedup, 10% is going into Pyston-lite, 10% was done independently by the CPython team between 3.8 and main, and the remaining 10% we are hoping to contribute back upstream."

To compare with Javascript: if I remember, as it appeared, V8 JIT was orders of magnitude faster, compared to the interpreted code.

Post reply on HN