[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: g++ optimization problem for branch statements conditioned with cons
John V. Shahid
Re: g++ optimization problem for branch statements conditioned with const bool
Mon, 08 Oct 2007 22:24:05 -0400
On Sun, 2007-10-07 at 08:38 -0700, PengYu.UT@gmail.com wrote:
> I have the following test program and compiled it with the -O3 in g++
> (. In "one_branch()" function, the two loops run in the same among of
> the time, which means the if statement in the first loop is not
Of course, this is a constant expression (i.e. b is always true). The
compiler might optimize it even without the -O3 option.
> In "two_branches" function, the last loop runs a lot faster
> than the other three loops, which means that the compiler does not
> optimize enough for the other three cases. I'm wondering if g++ can do
> better in those three cases. Is it a bug of g++?
Huh, you have saved two memory access for each iteration by using a
constant index (i.e., 0 instead of n). You expected the time to be less,
didn't you !!! The other three cases are consuming equal amount of time,
since the compiler is optimizing (and removing) the if statements, and
also optimizing the multiple addition operations.
John V. Shahid <firstname.lastname@example.org>