-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hello All,
In many C books I have come across the statement saying ++ and --
operators are more efficient.
However when I look at the assembler code for ++ and +1 both look
same.
Are there any scenarios where in ++ is more efficient?
Or is it just that above statement does not hold good for modern
compilers.
Thanks
Sac
Any optimizing compiler, its optimizations turned on, will treat ++ and
+=1 the same way. If INC is more efficient (eg. if we are not using
SPARC, for instance, in which INC doesn't particularly exist), code
using INC will most probably be generated.
Of course, I would advise not bothering with such things. Your compiler
can take care of such simple optimizations.
I was working on a project in school last term, and one lovely
individual who was working with me was told that ++i was more efficient
than i++ (which is true, because i++ must create a copy of the original
i to return for evaluation). In trying to optimize the code, he simply
replaced all the increment statements in his for loops with ++i where
they were formerly i++, breaking all the code and causing me hours of
wasted time. Optimizing code in this manner is rarely worth the things
like this caused by it.
- --
- --Falcon Darkstar Kirtaran
- --
- --OpenPGP: (7902:4457) 9282:A431
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla -
http://enigmail.mozdev.org
iQIcBAEBAgAGBQJJajIGAAoJEKmxP9YxEE4rXqoP/3PrfnPtopEOr0hNDSHDkTPY
yrSEuSGZPGqvE9eqISAaUyZc/bSbnwgJHb5sVe9qcuVPM4LLviHppT5O59RjfFm+
5WXzlhXDoRGKEI9FnlUgGT+cqAxos6S0gLVS5NEaL8xk+zaU3RF9RJzEPIEVp6xI
yWgZv9p/7VoaQWlJQTVoauqgDYH/ZEWc/WrhnS7EnxMp6SFqfyBLJT06FLgdQd/z
6DuMlaZYbU/Es4G+ezj4bJreLPY+vT74p3whn/ddgkCzivExjNwo7bz8e8Fy4MJ6
dpQKwh6JQAxeFfP0OR68O4CzLq0IK04sQQDt3gnJBRDvNXjNfomn3ftkF1lerHLZ
LETsF9v1c3TIC5K0tNj9QlsdTJi4zCh+YSwtZUWbIhH9mUpwqbLvXHJt0LIVnyAu
cLSFiXwriMI7vNZWWNKhUx8fQ6JbfxPtCfS9SEMIBaMBFQ03CUWvb6XLHOXfTUhB
AkU//m0hjMXYg3yDFBmsFbTEZnBjCZM3t2FfAaDtAS98dHA073YW80KwIsikYRXi
sB1TidQzdF9seUkfhbwalcXZYOKvVN3r7SislsW7RyLCxbq0qtRkzsEzIw6VEgng
FP4qRpWR+pgYKtDbSJRXDrVJhy9bQPBJbvV7qOpZ1O3ZCqAf3EgCXCMMBSnfQ1X5
gvJXupW7c+IhGbk66iWk
=aErl
-----END PGP SIGNATURE-----