help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

g++ optimization problem for branch statements conditioned with const bo


From: address@hidden
Subject: g++ optimization problem for branch statements conditioned with const bool
Date: Sun, 07 Oct 2007 08:38:32 -0700
User-agent: G2/1.0

Hi,

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
tested. 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++?

Program output:

0.31 sec
0.3 sec
-1908168351
0.38 sec
0.38 sec
0.38 sec
0.24 sec
2078732833

PS: I use the follow version of g++
g++ (GCC) 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There
is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.

Thanks,
Peng

#include <iostream>
#include <boost/timer.hpp>

const bool b = true;

const bool c = true;

void one_branch() {
  const int L = 200000;
  const int N = 1000;
  int x[N];
  int y[N];

// flush cache
  for(int l = 0; l < L; ++ l)
    for(int n = 0; n < N; ++ n)
      if(b)
        x[n] += y[n];

  for(int l = 0; l < L; ++ l)
    for(int n = 0; n < N; ++ n)
      if(b)
        x[n] += y[n];

  boost::timer this_timer;
  this_timer.restart();
  for(int l = 0; l < L; ++ l)
    for(int n = 0; n < N; ++ n)
      if(b)
        x[n] += y[n];
  std::cout << this_timer.elapsed() << " sec" << std::endl;

  this_timer.restart();
  for(int l = 0; l < L; ++ l)
    for(int n = 0; n < N; ++ n)
      x[n] += y[n];
  std::cout << this_timer.elapsed() << " sec" << std::endl;

  int sum = 0;
  for(int n = 0; n < N; ++ n)
    sum += x[n];
  std::cout << sum << std::endl;
}

void two_branches() {
  const int L = 200000;
  const int N = 1000;
  int x[N];
  int y[N];
  int z[N];

// flush cache
  for(int l = 0; l < L; ++ l)
    for(int n = 0; n < N; ++ n) {
      if(b)
        x[n] += y[n];
      if(c)
        x[n] += z[n];
    }

  for(int l = 0; l < L; ++ l)
    for(int n = 0; n < N; ++ n) {
      if(b)
        x[n] += y[n];
      if(c)
        x[n] += z[n];
    }

  boost::timer this_timer;
  this_timer.restart();
  for(int l = 0; l < L; ++ l)
    for(int n = 0; n < N; ++ n) {
      if(b)
        x[n] += y[n];
      if(c)
        x[n] += z[n];
    }
  std::cout << this_timer.elapsed() << " sec" << std::endl;

  this_timer.restart();
  for(int l = 0; l < L; ++ l)
    for(int n = 0; n < N; ++ n) {
      if(b && c)
        x[n] += y[n] + z[n];
    }
  std::cout << this_timer.elapsed() << " sec" << std::endl;

  this_timer.restart();
  for(int l = 0; l < L; ++ l)
    for(int n = 0; n < N; ++ n) {
      if(b && c)
        x[n] += y[n];
        x[n] += z[n];
    }
  std::cout << this_timer.elapsed() << " sec" << std::endl;

  this_timer.restart();
  for(int l = 0; l < L; ++ l)
    for(int n = 0; n < N; ++ n)
      x[n] += y[0] + z[0];
  std::cout << this_timer.elapsed() << " sec" << std::endl;

  int sum = 0;
  for(int n = 0; n < N; ++ n)
    sum += x[n];
  std::cout << sum << std::endl;
}

int main(){
  one_branch();
  two_branches();
  std::cout << std::endl;
}



reply via email to

[Prev in Thread] Current Thread [Next in Thread]