On Tue, Oct 12, 2021 at 10:41 PM Jeremiah Vivian nohackingofkrowten@gmail.com wrote:
So `ord` is already a really fast function with (last check before this thread was posted) 166 nsec per loop. But I'm wondering... doing `ord(a)` produces this bytecode:
2 4 LOAD_NAME 1 (ord) 6 LOAD_NAME 0 (a) 8 CALL_FUNCTION 1 10 POP_TOP 12 LOAD_CONST 1 (None) 14 RETURN_VALUE
But doing `+a` only produces this:
2 4 LOAD_NAME 0 (a) 6 UNARY_POSITIVE 8 POP_TOP 10 LOAD_CONST 1 (None) 12 RETURN_VALUE
So an operator has its own bytecode, doesn't need to `LOAD_*` a function, and doesn't have the impact on performance when converting arguments to the format of the (after `POP()`ing every argument) TOS function and then calling that function. And also, the unary `+` of strings only copies strings, which should be redundant in most cases. Maybe `ord` can take the place of unary `+` in strings?
-1. It's unnecessary optimization for an uncommon case, abuse of syntax (it's even worse than JavaScript using +"123" to force it to be a number), and illogical - why should +"a" be the integer 97?
ChrisA