g++ Long compile times

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

 



The program below is not very smart and repeats a pattern over and over (it is a simplification of a program that is automatically generated by a modeling language). It seems that the compiler is also not very smart and is repeating some calculations over and over.

When I use g++ to compile the program with
    EXTRA_CLASS_OBJECTS=0
the compile time is about 3 seconds. When
    EXTRA_CLASS_OBJECTS=1
the compile time is about 12 seconds.
(See the end of this message for the output of gcc -v on my system.)

The attached file gcc_slow.sh is a bash script that can be used to test this
on any system. If you execute
     ./gcc_slow.sh
you will get a help message that describes how it works.

Are there compile time options for gcc that can significantly reduce the g++
compile for EXTRA_CLASS_OBJECTS=1 case in the example program below ?
-------------------------------------------------------------------------

# include<cstdlib>
class mydouble {
public:
      double value_;
      mydouble&  operator =(double right);
      mydouble operator +(double right);

# if EXTRA_CLASS_OBJECTS
      size_t id_;
      mydouble(void);
      mydouble(size_t x);
      mydouble(int x);
      mydouble(double x);
# endif
};
mydouble mysum(void)
{
      mydouble x_0;
      x_0 = 0;
      mydouble x_1;
      x_1 = x_0 + 1.;
      mydouble x_2;
     ...
      mydouble x_7999;
      x_7999 = x_7998 + 7999.;
      return x_7999;
}
-------------------------------------------------------------------------
Using built-in specs.
Target: i686-pc-cygwin
Configured with: /gnu/gcc/releases/packaging/4.3.4-3/gcc4-4.3.4-3/src/gcc-4.3.4/configure --srcdir=/gnu/gcc/releases/packaging/4.3.4-3/gcc4-4.3.4-3/src/gcc-4.3.4 --prefix=/usr --exec-prefix=/usr --bindir=/usr/bin --sbindir=/usr/sbin --libexecdir=/usr/lib --datadir=/usr/share --localstatedir=/var --sysconfdir=/etc --infodir=/usr/share/info --mandir=/usr/share/man --datadir=/usr/share --infodir=/usr/share/info --mandir=/usr/share/man -v --with-gmp=/usr --with-mpfr=/usr --enable-bootstrap --enable-version-specific-runtime-libs --with-slibdir=/usr/bin --libexecdir=/usr/lib --enable-static --enable-shared --enable-shared-libgcc --disable-__cxa_atexit --with-gnu-ld --with-gnu-as --with-dwarf2 --disable-sjlj-exceptions --enable-languages=ada,c,c++,fortran,java,objc,obj-c++ --disable-symvers --enable-libjava --program-suffix=-4 --enable-libgomp --enable-libssp --enable-libada --enable-threads=posix --with-arch=i686 --with-tune=generic --enable-libgcj-sublibs CC=gcc-4 CXX=g++-4 CC_FOR_TARGET=gcc-4 CXX_FOR_TARGET=g++-4 GNATMAKE_FOR_TARGET=gnatmake GNATBIND_FOR_TARGET=gnatbind AS=/opt/gcc-tools/bin/as.exe AS_FOR_TARGET=/opt/gcc-tools/bin/as.exe LD=/opt/gcc-tools/bin/ld.exe LD_FOR_TARGET=/opt/gcc-tools/bin/ld.exe --with-ecj-jar=/usr/share/java/ecj.jar
Thread model: posix
gcc version 4.3.4 20090804 (release) 1 (GCC)

#! /usr/bin/bash
# -----------------------------------------------------------------------
# exit on any error
set -e
#
usage='
usage: ./gcc_slow.sh n_pattern extra_class_objects

Purpose:
Demonstrate that when compiling with g++, and a simple source code pattern
is repeated many times, defining extra class objects that are not used by the 
pattern significantly slows down the compilation.

n_pattern:
Number of times the pattern is repeated.

extra_class_objects:
If this argument is 1, the extra class objects are included.
If it is 0, the extra class objects are not included.
(0 and 1 are the only valid choices for extra_class_objects.)

Files:
This command creates (or overwrites) the files mysum.cpp and mysum.o
in the current working directory. 

Short Example:
First try running the following case 
	./gcc_slow.sh 4 0
and look at the file mysum.cpp. Then run
	./gcc_slow.sh 4 1
and see the difference in mysum.cpp.

Test Cases:
Then try running the following timing test of the compilation:
	./gcc_slow.sh 8000 0
	./gcc_slow.sh 8000 1
Here are some tests result for g++ (GCC) 4.3.4:
gcc_slow: n_pattern=8000, extra_class_objects=0, compile_time=  3.484s
gcc_slow: n_pattern=8000, extra_class_objects=1, compile_time=  11.890s
'
if [ "$2" != '0' ] && [ "$2" != '1' ]
then
	echo "$usage"
	exit 1
fi
n_pattern="$1"
extra_class_objects="$2"
# -----------------------------------------------------------------------
cat << EOF > mysum.cpp
# include <cstdlib>
class mydouble {
public:
	double value_;
	mydouble& operator =(double right);
	mydouble operator +(double right);

# if $extra_class_objects
	size_t id_;
	mydouble(void);
	mydouble(size_t x);
	mydouble(int x);
	mydouble(double x);
# endif
};
mydouble mysum(void)
{
	mydouble x_0;
	x_0 = 0;
EOF
i_last='0'
i_next='1'
while [ "$i_next" -lt "$n_pattern" ]
do
	echo "	mydouble x_$i_next;"               >> mysum.cpp
	echo "	x_$i_next = x_$i_last + $i_next.;" >> mysum.cpp
	i_last="$i_next"
	let i_next++
done
echo "	return x_$i_last;" >> mysum.cpp
echo "}"                     >> mysum.cpp
# -----------------------------------------------------------------------
# Use the flag -ftime-report to get information about where compiler
# is spending its time.
#
tmp="gcc_slow: n_pattern=$n_pattern"
tmp="$tmp, extra_class_objects=$extra_class_objects"
TIMEFORMAT="$tmp, compile_time=	%Rs"
echo "gcc_slow: time g++ -c mysum.cpp"
time g++ -c mysum.cpp


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux