Live data from Hacker News

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

codeconfessions.substack.com

161–170 of 224 posts

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

#161
post #49
post #7

Earlier quoted context omitted.

It shoud've been possible to establish the lower and upper bounds.

it's about as possible as solving the halting problem if you allow for operator overloading.

No. All you need to do is use a code coverage tool to find out which lines are run.

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

#162
post #17
post #5

Sounds like it would be easier to just use C anyway.

Yes, but C is a much more arcane and complicated language. Case in point: instead of writing a+b in Python, in C you would have to write a+b (Jokes aside, it would really be more complicated in C if a and b were actually strings or lists.)

a+b in Python adds two numbers. a+b in C adds the numbers if their sum is below a certain size, if not then it's undefined behaviour and destroys the security of your entire program.

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

#163

Earlier quoted context omitted.

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

> 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. I like Django. I need to process data on the server side and like to write that in python because it is mor…

This is the problem with SQL queries and Django Forms I am talking about

https://stackoverflow.com/questions/17330158/django-how-to-u...

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

#164
post #158

Earlier quoted context omitted.

> for the use case that Python is intended for Where is this single use case intent articulated, by whom, and what year was it? What is the use case? Today, it seems that Python is pitched for almost everything, short of ethernet drivers.

From https://www.python.org/about/ (mouse over the "About" menu) : 'Python is a programming language that lets you work more quickly and integrate your systems more effectively.'

This is one of the biggest misunderstandings in computing for the last decade, I feel.

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

#165
post #158

Earlier quoted context omitted.

> for the use case that Python is intended for Where is this single use case intent articulated, by whom, and what year was it? What is the use case? Today, it seems that Python is pitched for almost everything, short of ethernet drivers.

From https://www.python.org/about/ (mouse over the "About" menu) : 'Python is a programming language that lets you work more quickly and integrate your systems more effectively.'

That's not a specific use case. That's like brochure pitch.

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

#166

Earlier quoted context omitted.

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

> for the use case that Python is intended for Where is this single use case intent articulated, by whom, and what year was it? What is the use case? Today, it seems that Python is pitched for almost everything, short of ethernet drivers.

>Today, it seems that Python is pitched for almost everything, short of ethernet drivers.

I think the "Python is pitched for almost everything" in that sentence shows a misinterpretation of gp's phrasing of "use case".

The "use case" isn't about different subject matter domains as if it was a claim about using Python as a universal language for writing database kernels or AAA games.

Instead, the "use case" is about the 2-level 2-language architecture of (1) a high-level scripting language and (2) extension modules that can be written in low-level C and imported into the interpreter. That's the "glue language" + "C Language" to combine the strengths of each language approach. (In contrast, Julia took approach of designing a language that was "fast enough" to avoid the "2 languages issue".)

>Where is this single use case intent articulated, by whom, and what year was it? What is the use case?

The Python "ergonomics use case" (not "domains use case") was originated by Python's inventor Guido van Rossum from the beginning in 1991. A clone of the 1991 Python source code has Guido's commentary for importing C modules:

https://github.com/smontanaro/python-0.9.1

Modern frameworks and libraries like TensorFlow and Pytorch continue the same use case of "high-level script glue code calling low-level C code" that was there in 1991. You can't write a tight cpu loop in pure Python code to paint a 60fps video game. That's not Python's intended use case. That philosophy is why a library like Tensorflow only has Python code for users to "glue" together the neural network graph which then calls out to the C++ code for the expensive cpu loops of backpropagation, gradient descent, etc.

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

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

Hash function security is certainly a concern due to hash flooding attacks which force worst case performance hash table lookup and leads to denial of service. https://peps.python.org/pep-0456/

A lot of code never sees data input by untrusted sources. Just let me ignore the possible attacks and run python faster on my own data.

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

#168
post #166

Earlier quoted context omitted.

> for the use case that Python is intended for Where is this single use case intent articulated, by whom, and what year was it? What is the use case? Today, it seems that Python is pitched for almost everything, short of ethernet drivers.

>Today, it seems that Python is pitched for almost everything, short of ethernet drivers. I think the "Python is pitched for almost everything" in that sentence shows a misinterpretation of gp's phrasing of "use case". The "use case" isn't about different subject matter domains as if it was a claim about using Python as a universal language for writing database kernels or AAA games. Instead, the "use case" is about t…

I liked it back when we used "use case" for that concept with the actor executing a concrete interaction with a system to achieve a specific goal, and "architecture" for how the pieces fit together, without mixing or equating the two. :)

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

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

I really enjoined working in Lua.

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

#170

Earlier quoted context omitted.

I wonder what percent of this audience got the tootsie-roll reference. Does anyone under 40 know it?

I am about to turn 38 and I got it, so it's at least slightly lower than 40.

Hey Steve, loved your work on Rust and your goodbye letter. Nice to see you in my comment chain
Post reply on HN