c - Which is faster: Increment or equation with addition arithmetic -


example:

a : ++i; b : i++; c : += 1; d : = + 1; 

assuming each of them abcd called simultaneous, 1 of them performed first ?

in particular example 4 expressions have exact same externally observable result competent compiler should generate exact same code them.

the compiler doesn't slavishly read code , generate few instructions each statement, compiler reasons result of code should according standard , generates code needed whole program behave required. therefore asking performance questions single statements meaningless. let me show example:

void foo(unsigned int a, unsigned int b) { unsigned int = * b; } void bar(unsigned int a, unsigned int b) { unsigned int = + b; } 

which 1 faster? function foo or bar? many "of course multiplication slower", answer is: both equally fast because simple dead store optimization see nothing uses i, there's no need compute it, compiler can optimize functions down nothing. let's try it:

$ cat > foo.c void foo(unsigned int a, unsigned int b) { unsigned int = * b; } void bar(unsigned int a, unsigned int b) { unsigned int = + b; } $ cc -s -fomit-frame-pointer -o2 foo.c $ cat foo.s [... edited out irrelevant spam make more readable ...] _foo:                                   ## @foo     retq  _bar:                                   ## @bar     retq 

the instruction in both functions retq returns function.


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -