Discussion:
Is it helpful for a compiler to optimize?
Krzysztof
2012-06-03 06:38:00 UTC
Permalink
Let's have a below code:

struct st
{
int iv;
/*...*/
};

void afun(struct st *stp)
{
stp->iv++;
printf("%d\n",stp->iv);
}

Is it helpful for a compiler to do better optimization if I write the
function as this?

void afun(struct st *stp)
{
int liv=stp->iv++;

printf("%d\n",liv);
}

In other words, is it better from optimization point of view to do
assignments structure members values to local variables then do what you
need to and then assign them back to member variables?
--
Regards
Krzysztof J.


--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to ***@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Krzysztof
2012-06-03 06:59:13 UTC
Permalink
Post by Krzysztof
void afun(struct st *stp)
{
int liv=stp->iv++;
printf("%d\n",liv);
}
Should be: int liv=++stp->iv; of course for equivalence.
--
Regards
Krzysztof J.


--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to ***@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Srinivasa T N
2012-06-04 14:09:07 UTC
Permalink
Why don't you generate the assembly code for both the versions with -s
and -O<lvl>?

Regards,
Seenu.
Post by Krzysztof
Post by Krzysztof
void afun(struct st *stp)
{
int liv=stp->iv++;
printf("%d\n",liv);
}
Should be: int liv=++stp->iv; of course for equivalence.
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to ***@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Jesse Ruffin
2012-06-03 08:03:44 UTC
Permalink
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

As far as I understand it the compiler's break down the code into
abstract syntax graphs then use a number of graph optimizations to
reduce it into an optimized state. The examples will almost certainly
produce equivalent binary output.

Rather than being concerned about how best to help the compiler
optimize I usually worry about the most understandable way to write
the function. Compilers are really, really good at their job, and the
completely unoptimized version is the one your going to see when you
are debugging -O0 builds.

- -Jesse
struct st { int iv; /*...*/ };
void afun(struct st *stp) { stp->iv++; printf("%d\n",stp->iv); }
Is it helpful for a compiler to do better optimization if I write
the function as this?
void afun(struct st *stp) { int liv=stp->iv++;
printf("%d\n",liv); }
In other words, is it better from optimization point of view to do
assignments structure members values to local variables then do
what you need to and then assign them back to member variables?
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.17 (MingW32)

iQIcBAEBAgAGBQJPyxpgAAoJEO7eCtQ2Ge2p6bwQAJBOlV6cJnKfrhfE44DGBHQA
U8nY2weVlVX51YbnG9GAtHNbFtEIy55Z7SZSp9k44KLyQsu3Zj/E1UFQbdTsqVwQ
Gy8SJHu114x9RQgk2HvbLPp2+uwjNo2BPQf03avrD0ERPvAa1Uyfa4Q/labwWoDr
/rFV+90y3q22uEZZe85xIzuGHOLEE8z2qtZko27bvfQM9i+QYNhwCppelIci8Jr/
DhoiMs8O7EgylWaHQdZgbr7eUOjXYOdi1ypXLC5/j2S++vaXNmnSCKIXrQXvBNKc
D87TbjlqaW+E6IsQYC9uaND/mSfoWSHTwLRQuQrdYQFxlvaHHNoMIf0GICSLqoEq
vizMtu7LuWYirwQ6VJJ/CF1GKFD2jIIEE2OlQrA9fGdxLfgjU/VUG3WWc36HJYd7
Et7XGM5DsUUr0LSWUjbB4AtyKhsfiV7V4q7sgGjClrJ8dr7AqtG61rRtebG7CCL2
GBhw603yQCDHkclQaNApCU0TOYG6Rh3K+8xF+iKN6haj4bi6BduRUnxmLdZuc8Bn
VYZhT7833cBBpdkXPpMLdubo7B8qIG9/01oXXs/b7+q+jG1EF32L8dsmh5XgafoF
iy7DidF0y+oyDUZO82q2omLTMTxcRjDRyh+n8WI1/E5XG4FvqyO+xEHSC0YXe3Nn
SWvd/4lS2YEaExH17uzF
=V4At
-----END PGP SIGNATURE-----
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to ***@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Loading...