Question

tl;dr: When I compile my code one way, the executable runs quickly. When I use my makefile it is ~10 times slower (executable speed, not compile time).

When I compile the following code (which uses the Eigen package):

#include <Eigen/Dense>          // For matrix math
#include <iostream>

using namespace std;
using namespace Eigen;

// Loop an infinite number of times, computing dot products.
int main(int argc, char * argv[]) {
    setNbThreads(16);
    initParallel();     // Tell Eigen that we're going to be multithreaded.

    int n = 100;
    VectorXf a(n), b(n);
    for (int counter = 0; true; counter++) {
        a[0] = a.dot(b) / n;
        if ((counter + 1) % 10000000 == 0) {
            cout << counter / 10000000  << endl;
        }
    }
}

using the line:

g++ *.cpp -o exe -I ./PathToEigen -std=c++11 -O2 -DNDEBUG -msse2

it runs very quickly. If I use the makefile below, the resulting executable is about 10 times slower. What am I doing wrong?

PROGRAM = EXE

INCLUDEDIRS = -I ./PathToEigen
CXXSOURCES = $(wildcard *.cpp)
CXXOBJECTS = $(CXXSOURCES:.cpp=.o)  # expands to list of object files
CXXFLAGS = -w $(INCLUDEDIRS)
CXX = g++

#
# Default target: the first target is the default target.
# Just type "make -f Makefile.Linux" to build it.
#

all: $(PROGRAM)

#
# Link target: automatically builds its object dependencies before
# executing its link command.
#

$(PROGRAM): $(CXXOBJECTS)
    $(CXX) -o $@ $(CXXOBJECTS) -std=c++11 -O2 -DNDEBUG -msse2

# Clean target: "make -f Makefile.Linux clean" to remove unwanted objects and executables.
#

clean:
    $(RM) -f $(CXXOBJECTS) $(PROGRAM)

#
# Run target: "make -f Makefile.Linux run" to execute the application
#             You will need to add $(VARIABLE_NAME) for any command line parameters 
#             that you defined earlier in this file.
# 

run:
    ./$(PROGRAM) 
Was it helpful?

Solution

Because you're not compiling with any optimizations. (There's no -O2 in CXXFLAGS, the -O2 in the $(PROGRAM) rule only applies to the link step.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top