From: | cdg@nullstone.com (Christopher Glaeser) |
Newsgroups: | comp.compilers |
Date: | 16 Mar 1996 00:01:10 -0500 |
Organization: | Compilers Central |
References: | 96-03-006 96-03-091 |
Keywords: | C, performance |
> [Do people really not use const and static? -John]
1) const - Granted, const is useful in situations where you want to
inhibit stores and isolate potential errors at compile time. However,
using const as a replacement for constant macros has performance
penalties for many C compilers. In particular, many C compilers will
load the value of "one" in the example below, rather than use the
value "1".
#define ONE 1
const int one = 1;
int f()
{
x = ONE;
y = one;
}
2) static - Static can cause significant performance problems for many
C compilers. In particular, few C compilers can keep static variables
in registers over a region of code. This is most notable when using
f2c to benchmark C compilers with FORTRAN benchmarks. Consider the
following code fragment.
SUBROUTINE F()
INTEGER I
DO 10 I = 1, 100
10 CONTINUE
RETURN
END
f2c gnerates the following output.
int f_()
{
static integer i;
for (i = 1; i <= 100; ++i) { }
return 0;
}
Benchmarks such as Livermore Loops run about three times slower when
these variables are delcared static (versus auto) for compilers that
can not keep static variables in registers.
Static inhibits many other optimizations as well. Consider:
x = a + b;
y = a + b;
Some compilers will eliminate the CSE if a and b are auto, but will
not eliminate the CSE if a or b are static.
Regards,
Christopher Glaeser cdg@nullstone.com
Nullstone Corporation http://www.nullstone.com
--
Return to the
comp.compilers page.
Search the
comp.compilers archives again.