When I compile the program below with EXTRA_CLASS_OBJECTS equal to 0, the compile time is about 3 seconds. When EXTRA_CLASS_OBJECTS is 1, the compile time is about 12 seconds.
The program is not very smart and repeats a pattern over and over 
(actually it is a simplification of a program that is automatically 
generated by a modeling language). It seems to me that the compiler is 
also not very smart and is repeating some calculation over and over.
The attached file gcc_slow.sh is a bash script that can be used to test 
this on your system. If you execute
     ./gcc_slow.sh
you will get a help message that describes how it works.
===================================
# 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;
}

#! /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

Reply via email to