3 ms·
That would be a nice optimization. Here's what Python does 0 LOAD_FAST 0 (x) 2 LOAD_CONST 1 (1000)
by tln 5y ago
That would be a nice optimization. Here's what Python does
0 LOAD_FAST 0 (x)
2 LOAD_CONST 1 (1000)
4 COMPARE_OP 0 (<)
6 POP_JUMP_IF_FALSE 18
8 LOAD_FAST 0 (x)
10 LOAD_CONST 2 (1)
12 BINARY_ADD
14 STORE_FAST 0 (x)
16 JUMP_ABSOLUTE 0
18 LOAD_CONST 0 (None)
Python's VM is very slow, but note only one arg per op and the compare + jump are not fused.
BTW, if we're considering "advanced" optimization might as well elide the whole loop :) EG https://godbolt.org/z/n3GznqKsM https://godbolt.org/z/n3GznqKsM
test(int):
push rbp
mov rbp, rsp
mov DWORD PTR [rbp-4], edi
jmp .L2
.L3:
add DWORD PTR [rbp-4], 1
.L2:
cmp DWORD PTR [rbp-4], 999
jle .L3
nop
pop rbp
ret
- mst 5y agoWell, sure, but I was thinking along the lines of "reserving a spot on the stack and reserving a register seem like about the same level of clever", especially thinking about how mixed register/stack argument passing works in even relatively stupid C compilers. Eliding the entire loop seems relatively similarly clever for either architecture, but would've counted as cheating in the frame I was pondering from. Still neat to see even if x86 asm continues to make my eyes cross ;)
- exikyut 5y ago(Why is there a "nop" near the end? What does it do?)