Programming Language/cpp

Is ++i better than i++?

snp0301 2025. 10. 11. 21:50

Assuming that prefix and postfix operations perform the same computation,
++i is generally more efficient than i++ in terms of cost.

Since i++ requires storing the original value in a temporary register, ++i is slightly cheaper.

++i

    add rax, 1        ; i = i + 1

i++

    mov rcx, rax      ; old = i   (temporary value)
    add rax, 1        ; i = i + 1 (original value)