Live data from Hacker News

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

codeconfessions.substack.com

1–10 of 224 posts

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

#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 software is exposed to the web. None of my Python programs are, so assuming all this is true, I'd much prefer to have a "I'm offline, please run twice as fast!" flag (or env variable).

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

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

It looks like you can disable the slower randomized hashing yourself by setting PYTHONHASHSEED to 0. Though I don't know if there's further speedup to be had by using a different hash implementation.

https://docs.python.org/3/using/cmdline.html#envvar-PYTHONHA...

The original issue: https://bugs.python.org/issue13703

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

#7
post #3
post #2

What's the answer?

Running `__radd__(tyepof(b) b)` on `a` seems like a complicated problem. So: Many LoC? Or, the generic, useless but correct, answer: it depends (as the linked article said, too)

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

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

#8
post #5

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

Easier for whom?

The whole point of high level languages is that you put in effort upfront to make everyone elses job easier.

By you logic, using machine code directly onto toggle switches is easiest. No assembler to write, no test editor to write.....

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

#9
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'm aware that Rust has something similiar for things like `std::collections::HashMap`. By default:

> The default hashing algorithm is currently SipHash 1-3, though this is subject to change at any point in the future. While its performance is very competitive for medium sized keys, other hashing algorithms will outperform it for small keys such as integers as well as large keys such as long strings, though those algorithms will typically not protect against attacks such as HashDoS.

https://doc.rust-lang.org/std/collections/struct.HashMap.htm...

However, Rust also lets you pick or implement your own hash algorithm if you want to optimise for your usecase.

Post reply on HN