With modern compilers, these will not make a difference.
See here for a read-up on common compiler optimizations: https://queue.acm.org/detail.cfm?id=3372264
However, as @asteroids-with-wings rightly points out, these don't even come into play here.
What actually happens is very likely to be compiler-specific, but you can check what they create by looking at the assembly code.
Example code:
test.cpp:
int main(int argc, char **argv) {
int i, j;
i = j = 0;
}
test2.cpp:
int main(int argc, char **argv) {
int i, j;
i = 0;
j = 0;
}
I compiled them with the following options:
clang test.cpp -O0 -save-temps=obj -o test_exec
clang test2.cpp -O0 -save-temps=obj -o test_exec2
-O0
is to disable optimizations, -save-temps=obj
will keep the generated assembly around for inspection.
This provides the following two assembly files:
test.s:
.text
.file "test.cpp"
.globl main # -- Begin function main
.p2align 4, 0x90
.type main,@function
main: # @main
.cfi_startproc
# %bb.0:
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset %rbp, -16
movq %rsp, %rbp
.cfi_def_cfa_register %rbp
xorl %eax, %eax
movl %edi, -4(%rbp)
movq %rsi, -16(%rbp)
movl $0, -24(%rbp)
movl $0, -20(%rbp)
popq %rbp
.cfi_def_cfa %rsp, 8
retq
.Lfunc_end0:
.size main, .Lfunc_end0-main
.cfi_endproc
# -- End function
.ident "clang version 11.0.0"
.section ".note.GNU-stack","",@progbits
.addrsig
test2.s:
.text
.file "test2.cpp"
.globl main # -- Begin function main
.p2align 4, 0x90
.type main,@function
main: # @main
.cfi_startproc
# %bb.0:
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset %rbp, -16
movq %rsp, %rbp
.cfi_def_cfa_register %rbp
xorl %eax, %eax
movl %edi, -4(%rbp)
movq %rsi, -16(%rbp)
movl $0, -20(%rbp)
movl $0, -24(%rbp)
popq %rbp
.cfi_def_cfa %rsp, 8
retq
.Lfunc_end0:
.size main, .Lfunc_end0-main
.cfi_endproc
# -- End function
.ident "clang version 11.0.0"
.section ".note.GNU-stack","",@progbits
.addrsig
As you can see in the diff:
2c2
< .file "test.cpp"
---
> .file "test2.cpp"
17d16
< movl $0, -24(%rbp)
18a18
> movl $0, -24(%rbp)
there is very little difference between the two codes.
The only real difference is in lines 17 + 18, where these two lines are swapped:
movl $0, -20(%rbp)
movl $0, -24(%rbp)
Even without optimization, the only difference here is the order in which the variables are initialized, otherwise the same thing happens.
Note: this holds true for your specific case of assigning a compile-time constant (0
). Results may differ for using run-time values from other variables.
As always in performance questions: Investigate what your compiler does, and profile the result - there may not be a single true answer.