J
James Kuyper
somenath wrote:
....
The text you cite is describing the difference between a=a+10 and a+=10.
The difference between three_more and better_three_more is that one uses
return x+=3; and the other uses return x+3, which is a quite different
issue.
The first pair of expression are semantically equivalent, and will
compile to the same code using a smart compiler; the second of the two
expressions may compile to better code when using a dumb compiler, but
that's a minor issue. The facts that a+=10 is marginally easer to type
and easier to understand (once you're used to it) is far more important
(this is even more important when 'a' is replaced by a long complicated
expression).
The difference between return x+=3; and return x+3; is that the first
nominally specifies a pointless change to a variable whose lifetime will
end before the changed value ever gets used. A smart compiler will
probably notice that fact and produce the same code for both statements.
However, it's still bad practice to use return x+=3; because a
maintenance programmer is likely to waste time wondering why you
specified the change to x, before realizing that it was just a coding error.
....
I would like to clarify my self why I requested so. I was reading one
C text book which is famous in our country it says as mentioned.
"These instructions increase directly specify the required information
so help in faster execution. 'C' makes
efficient use of this feature by providing compound statements for
which translation can be done directly to
its corresponding machine instruction. For example: ....
So according to this logic first function "int three_more(int x)"
may be faster then the "int better_three_more(int x)". Is it not
correct ?
The text you cite is describing the difference between a=a+10 and a+=10.
The difference between three_more and better_three_more is that one uses
return x+=3; and the other uses return x+3, which is a quite different
issue.
The first pair of expression are semantically equivalent, and will
compile to the same code using a smart compiler; the second of the two
expressions may compile to better code when using a dumb compiler, but
that's a minor issue. The facts that a+=10 is marginally easer to type
and easier to understand (once you're used to it) is far more important
(this is even more important when 'a' is replaced by a long complicated
expression).
The difference between return x+=3; and return x+3; is that the first
nominally specifies a pointless change to a variable whose lifetime will
end before the changed value ever gets used. A smart compiler will
probably notice that fact and produce the same code for both statements.
However, it's still bad practice to use return x+=3; because a
maintenance programmer is likely to waste time wondering why you
specified the change to x, before realizing that it was just a coding error.