简体   繁体   English

如果我在代码块中构建代码,为什么我的代码运行得更快?

[英]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++ -fltoLDFLAGS=$(CXXFLAGS)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果我使用codeBlocks进行编译,为什么我的代码运行得更快 - Why is my code running faster if i compiled it with codeBlocks 为什么 C++ 模板代码在我的 lambda 表达式中运行得更快? - Why C++ template code runs faster in my lambdas? 为什么在这种情况下,我的 Java 代码比我的 C++ 代码运行得快? - Why in this case, my Java code runs faster than my C++ code? 为什么我的 C++14 KosaRaju 算法在类似的编写代码运行得更快时得到 TLE - Why my C++14 KosaRaju algo getting TLE when a similar written code runs much faster 运行时近似:为什么代码 1 运行得比代码 2 快? - Runtime Approximation: Why code1 runs faster code 2? 为什么我的代码在代码块下而不在VS Studio中运行 - Why is my Code runing under codeblocks but not in VS Studio 代码在OSX上运行,代码块VMbox中的分段错误 - Code runs on OSX, Segmentation fault in Codeblocks VMbox 为什么多线程代码在更快的机器上运行得更慢? - Why multi-threaded code runs slower on faster machines? 为什么我的程序在1个线程上比在8个线程上运行更快? - Why my program runs faster on 1 thread than on 8. C++ 为什么我的代码块上没有显示输出? - Why there is no output showing on my codeblocks?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM