Live data from Hacker News

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

codeconfessions.substack.com

191–200 of 224 posts

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

#191
post #183

Earlier quoted context omitted.

Is there any direct comparison though? Because it can be alternatively argued that these dynamic languages needed new JIT techniques for performance, but they especially work well for more constrained languages like JS. I don't think Smalltalk was ever a good fit for number crunching, for example.

Better than CPython, regardless of how you put it. EDIT: Also to note, those languages powered whole graphical single workstations, with microcoded CPUs + JIT. "Efficient implementation of the smalltalk-80 system" https://dl.acm.org/doi/10.1145/800017.800542 https://computerhistory.org/blog/introducing-the-smalltalk-z... "Self-Confidence: How SELF Became a High-Performance Language" https://www.cs.cornell.edu/courses…

vs CPython

https://benchmarksgame-team.pages.debian.net/benchmarksgame/...

vs Node

https://benchmarksgame-team.pages.debian.net/benchmarksgame/...

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

#192
post #122

Earlier quoted context omitted.

In the late 1990s and early 200xs, there were a lot of claims that there was no such thing as a "slow language", that all languages can be run as quickly as C if you just built a sufficiently smart compiler and/or runtime. I haven't heard anyone make this claim in a while. The inability to speed up Python beyond a certain point despite a lot of clever approaches taken was probably a good chunk of the reason, the rema…

> In the late 1990s and early 200xs, there were a lot of claims that there was no such thing as a "slow language", that all languages can be run as quickly as C if you just built a sufficiently smart compiler and/or runtime. I think "slow language" never meant that way. It was more like a counterpoint to the claim that there are inherent classes of languages in terms of performance, so that some language is (say) 100…

> Most languages with enough optimization works can be made performant enough that it's no slower than 10x C.

Maybe if we take the slowest C program, CPython no slower than 50x ?

https://benchmarksgame-team.pages.debian.net/benchmarksgame/...

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

#193
post #181

Earlier quoted context omitted.

You should have added Ruby JITs into the explanation, with the related generated Assembly code. :)

Yeah that would be fun but I don't have a Ruby environment installed and the comment is already getting very long; if I ever turn this into a blog post someday maybe I will add that :)

You just happened to post a day after "Ruby 3.3.0-Rc1 Released" "many performance improvements especially YJIT"

https://news.ycombinator.com/item?id=38599293

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

#194

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.

That's only true for integers. For scientific applications you'd typically want floating points and Python's floats are just regular ieee-754 doubles (or whatever "double" meant to the compiler used to compile that python interpreter).

The disadvantage with floats is you can't do accurate computations due to having an inexact representation. Python has the same issues with floats.

For example: I do a lot of work with financial software and also some basic applied cryptography. And the essential rule is to never ever use floats. Where 'decimals' are needed you want them to be simulated using integers. Python has a module called decimal which I think helps mitigate some of these issues.

I've written some code to work with accurate, large precision numbers in Python and C before. Mostly the annoying part with this is having data types for the numbers that correspond well to database fields (like uint64) or are portable (in C its easiest if you can get a u128 but this type is very compiler-specific so some hacking may be needed.)

It's fun to work on code like this but definitely needs to be precise and have good test coverage. Writing your own math libraries that are going to be used for such important operations is hair-raising stuff.

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

#195
post #174

Earlier quoted context omitted.

I'd love to see an example of this if you happen to have one available. I hit a startup-time issue in a previous life, and I wish I had spent the time looking at it back then

A template repo can be found here https://github.com/JacksonKearl/cpython , but it does not implement an ideal malloc, just a baseline one - I am not sure if it is still being used as an assignment. The repo states that even this dummy implementation: > has a 60% faster startup as compared to base CPython, and in some test cases has marginally better runtime performance as well.

Thankyou so much!

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

#196
post #192

Earlier quoted context omitted.

> In the late 1990s and early 200xs, there were a lot of claims that there was no such thing as a "slow language", that all languages can be run as quickly as C if you just built a sufficiently smart compiler and/or runtime. I think "slow language" never meant that way. It was more like a counterpoint to the claim that there are inherent classes of languages in terms of performance, so that some language is (say) 100…

> Most languages with enough optimization works can be made performant enough that it's no slower than 10x C. Maybe if we take the slowest C program, CPython no slower than 50x ? https://benchmarksgame-team.pages.debian.net/benchmarksgame/...

CPython had many other concerns besides from performance, and PyPy is much closer to my quote I believe.

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

#197
post #191
post #183

Earlier quoted context omitted.

Better than CPython, regardless of how you put it. EDIT: Also to note, those languages powered whole graphical single workstations, with microcoded CPUs + JIT. "Efficient implementation of the smalltalk-80 system" https://dl.acm.org/doi/10.1145/800017.800542 https://computerhistory.org/blog/introducing-the-smalltalk-z... "Self-Confidence: How SELF Became a High-Performance Language" https://www.cs.cornell.edu/courses…

vs CPython https://benchmarksgame-team.pages.debian.net/benchmarksgame/... vs Node https://benchmarksgame-team.pages.debian.net/benchmarksgame/...

Exactly. Comparing with CPython is too easy and arguably unfair, Smalltalk should be compared with JS instead and I think it has no chance in that setting.

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

#198
post #185

Earlier quoted context omitted.

> In the late 1990s and early 200xs, there were a lot of claims that there was no such thing as a "slow language", that all languages can be run as quickly as C if you just built a sufficiently smart compiler and/or runtime. I think "slow language" never meant that way. It was more like a counterpoint to the claim that there are inherent classes of languages in terms of performance, so that some language is (say) 100…

"If you have used Python long enough you may know that Python originally had two types of classes" Yes, because that's also the era where this claim was flying around. I'd say that each individual person may have their own read on what the claim meant, but certainly the way it was deployed at anyone who vaguely complained that Python was kind of slow shows that plenty of people in practice read it as I've described..…

Yeah, I agree everyone may have different anecdotes, for my case though I heard more of the "Python as glue code" arguments and never heard that Python proper can be as fast as C. I've used Python since 2.3, so maybe that argument was more prevalent before?

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

#199

Earlier quoted context omitted.

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

If you want to be return absolutely any value from an iterator, an exception is indeed a reasonable choice though. Python generators came in much later, unlike JS for example.

If that is indeed a requirement, you let your iterator have an .at_end() test instead of trying to shoehorn it into the return value. Or have something like C++'s std::optional or Rust's Option. Or a special EndOfIteration object that you cannot return from an iterator (is that really so bad?). Iteration is extremely common, and should thus be based on as simple primitives as possible if you want things to go fast.
Post reply on HN