TL DR: use an initializer - it is never worse memset().
It depends on your compiler. It should not be slower than a call memset()(since a call memset()is one of the options available to the compiler).
, ; , , - , .
, , GCC :
#include <string.h>
int f1()
{
int a[32] = {0};
return a[31];
}
int f2()
{
int a[32];
memset(a, 0, sizeof a);
return a[31];
}
gcc -S -std=c11 :
f1:
.LFB0:
.file 1 "40786375.c"
.loc 1 4 0
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $8, %rsp
.loc 1 5 0
leaq -128(%rbp), %rdx
movl $0, %eax
movl $16, %ecx
movq %rdx, %rdi
rep stosq
.loc 1 6 0
movl -4(%rbp), %eax
.loc 1 7 0
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
f2:
.LFB1:
.loc 1 10 0
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
addq $-128, %rsp
.loc 1 12 0
leaq -128(%rbp), %rax
movl $128, %edx
movl $0, %esi
movq %rax, %rdi
call memset@PLT
.loc 1 13 0
movl -4(%rbp), %eax
.loc 1 14 0
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
, f1() rep stosq , f2() , , C. , memset() , , , , , .
a volatile, , (gcc -S -std=c11 -O3):
f1:
.LFB4:
.cfi_startproc
subq $16, %rsp
.cfi_def_cfa_offset 24
xorl %eax, %eax
movl $16, %ecx
leaq -120(%rsp), %rdi
rep stosq
movl 4(%rsp), %eax
addq $16, %rsp
.cfi_def_cfa_offset 8
ret
.cfi_endproc
f2:
.LFB5:
.cfi_startproc
subq $16, %rsp
.cfi_def_cfa_offset 24
xorl %eax, %eax
movl $16, %ecx
leaq -120(%rsp), %rdx
movq %rdx, %rdi
rep stosq
movl 4(%rsp), %eax
addq $16, %rsp
.cfi_def_cfa_offset 8
ret
.cfi_endproc
, .