How many lines of C it takes to execute a + b in Python
61–70 of 224 posts
Re: How many lines of C it takes to execute a + b in Python
#62Earlier 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?
>>> {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
#63Earlier 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
$ 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
#64This 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.
Re: How many lines of C it takes to execute a + b in Python
#65Earlier 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…
Re: How many lines of C it takes to execute a + b in Python
#66Earlier 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…
Re: How many lines of C it takes to execute a + b in Python
#67Re: How many lines of C it takes to execute a + b in Python
#68[flagged]
Re: How many lines of C it takes to execute a + b in Python
#69[flagged]