[英]Why my code runs faster if I build it in codeblocks?
I have created a small library (around 600 lines) in C++ in Codeblocks and I'm using OMP and O3 optimization to build it. 我已经在C ++中的代码块中创建了一个小型库(约600行),并且正在使用OMP和O3优化来构建它。 When I try to build the same code through the terminal with a Makefile with exactly the same options ( -fopenmp -O3 ) it runs around 3 times slower.
当我尝试通过带有完全相同选项( -fopenmp -O3 )的Makefile通过终端构建相同的代码时,它的运行速度大约慢了3倍。 I need to build it in various machines so I need to do the process through a terminal and not through Codeblocks.
我需要在各种机器上构建它,所以我需要通过终端而不是通过代码块来完成该过程。 Why is this happening?
为什么会这样呢? This is my Makefile if you're interested:
如果您有兴趣,这是我的Makefile:
CC=g++
CFLAGS=
LDFLAGS= -fopenmp -O3 -std=c++11
SOURCES=main.cpp CNNFunctions.cpp
OBJECTS=$(SOURCES:.cpp=.o) EXECUTABLE=cnn
all: $(SOURCES) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $@
.cpp.o:
$(CC) $(CFLAGS) $< -o $@
Because, contrary to your claim, you're not building it with the exact same options. 因为,与您的主张相反,您没有使用完全相同的选项来构建它。
Your CFLAGS
are empty, and those are the flags you're using for the compilation. CFLAGS
是空的,这些是您用于编译的标志。 You cannot resolve that by the time you get to the linking step. 到链接步骤时,您无法解决该问题。
Your Makefile
is wrong. 您的
Makefile
错误。 The optimization flags are relevant mostly at compile time. 优化标志主要在编译时相关。 Try at least:
至少尝试:
CXX=g++
CXXFLAGS= -Wall -fopenmp -O3 -std=c++11 -mtune=native
LDFLAGS= -fopenmp
SOURCES=main.cpp CNNFunctions.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=cnn
all: $(SOURCES) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(LINK.cpp) $(OBJECTS) -o $@
Optimization might matter at link time for Link-Time Optimization , for that use CXX=g++ -flto
and LDFLAGS=$(CXXFLAGS)
对于链接时优化 ,优化可能在链接时很重要,因为使用
CXX=g++ -flto
和LDFLAGS=$(CXXFLAGS)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.