void f() { ; }
void g() { }
void h() { __asm__("nop"); }
all result in almost identical assembly output (x86 64).
f
& g
both give
push rbp
mov rbp, rsp
nop
pop rbp
ret
while g
gives
push rbp
mov rbp, rsp
nop
nop
pop rbp
ret
(one extra nop
)
In my testing, it seems that a loop containing just __asm__("nop")
takes ~50% longer than ;
When using the -O1
flag or above, the same still applies - f
& g
become
ret
and h
becomes
nop
ret
When using a loop, e.g. for(;;){}
, the __asm__
still adds one extra instruction, which is understandable