Live data from Hacker News

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

codeconfessions.substack.com

61–70 of 224 posts

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

#62
post #48
post #16

Earlier quoted context omitted.

Python 3.11 appears to have switched to SipHash 1-3 for strings, from 2-4, following the lead of Rust and Ruby. https://github.com/python/cpython/issues/73596 However, Python does not use it for integers; >>> hash(10) 10 >>> hash(100) 100 >>> hash(2**61-2) == 2**61-2 True >>> hash(2**61-1) 0

That’s good though, right? Is there a reason for not using an identity hash (is that the right term?) for integers?

Don't use user-supplied integers on dicts or sets on Python:

>>> {i for i in range(10000)}

Takes 0.005s

>>> {i * sys.hash_info.modulus for i in range(10000)}

Takes 0.76s

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

#63
post #42
post #17

Earlier quoted context omitted.

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

It still isn't simple #! /Usr/bin/python Print(a+b) V #include Int main (){ Printf("%i", a+b); Return 0; } And printf is basically a DSL, so it still isn't 'simple' And this is assuming a+b fits into an integer

If you're being silly you don't need a lot of that:

    $ cat tmp.c
    main() {
       printf("%d\n", 3+4);
    }
    $ gcc -w -o tmp.out tmp.c && ./tmp.out
    7
You can even do away with types entirely, as long as you're working with ints:

    $ cat tmp.c
    foo(x) {
       return x+5;
    }
    bar() {
       return 4;
    }
    main() {
       printf("%d\n", foo(bar()));
    }
    $ gcc -w -o tmp.out tmp.c && ./tmp.out
    9
Rarely a good idea, though!

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

#64
post #37

This was quite interesting, but I’m disappointed it didn’t mention how many lines in C it actually took to run. Perhaps a profiler might help calculate this?

If you read the article you can see that some codepaths can invoke Malloc with all the follow-on effects like Kernel boundary crossings that this implies, it's thus quite random.

It would still make sense to give a number or a range.

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

#65
post #15

Earlier quoted context omitted.

Do you mean this post? https://www.reddit.com/r/Python/s/raofvsKCiz That speedup was disputed in the comments. I haven’t tried it myself though

https://www.reddit.com/r/Python/comments/mgi4op/comment/gswg... I’d use “thoroughly disproven” rather than “disputed”. I’m sure the hash function can be changed, but as various comments noted: - the benchmark was nonsensical - cpython caches string hashes, and “symbols” are interned, so outside of dynamic attribute access from dynamically constructed strings each hash for attribute purposes or namespace lookup is com…

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 gesture vaguely at their custom type and have it hashed correctly for any hash functions they need. The naïve approach here easily ends up doing a bad job either colliding hashes for correlated objects in a surprising way or emitting different hashes for equivalent objects because people who make a user defined type probably aren't hashing experts.

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

#66

Earlier quoted context omitted.

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…

It’s not entirely true that JS does not have magic methods. `valueOf` and `toString` can show up surprisingly deep into the resolution of operations, and recent JS has “well known symbols” to implement or override behaviour. However it is true that this is much, much less extensive than it is in Python. As of 3.12, section 3.3 (“special method names”) of the data model documentation lists 107 entries (although some o…

`valueOf` and `toString` are indeed fairly complex (and it's fun to consider when both are implemented ;-), but less of concern for tracing JIT engines because you can have efficient type-specialized implementations for most cases. Type specialization in Python is not that huge win...

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

#70
post #61

[flagged]

Didn't see that. I guess that's just uBO working as intended.

My uBO didn't catch them for the record. I just added the following rule to be sure:

    substack.com##[class^="frontend-components-SubscribePrompt-"]
Post reply on HN