joe*_*ler 17 c++ gcc profiling g++ c++11
我一直在研究遗传算法,我以前一直在使用g ++ 4.8.1编译参数
CCFLAGS=-c -Wall -Ofast -fopenmp -mfpmath=sse -march=native -std=gnu++11
Run Code Online (Sandbox Code Playgroud)
我没有使用c ++ 11的许多功能,并且有一个合理的分析系统,所以我替换了3-4行代码并让它编译而没有-std = gnu ++ 11
CCFLAGS=-c -Wall -Ofast -fopenmp -mfpmath=sse -march=native
Run Code Online (Sandbox Code Playgroud)
当我再次运行我的探查器时,我注意到除了我的排序功能之外,我几乎可以在任何地方看到约5%的性能提升,现在排序大约是两倍.(它是一个重载的运算符<在对象上)
我的问题是:
这两个版本之间存在哪些性能差异,并且预计c ++ 11在较新的编译器中会更快?
我也期待我正在使用的事实--Ofast正在扮演一个角色,我的假设是正确的吗?
更新:
正如评论中所建议的那样,我使用with和without -march = native再次运行测试
// Fast sort, slightly slower in other tests
CCFLAGS=-c -Wall -Ofast -fopenmp -mfpmath=sse -march=native -std=gnu++11
// Fast sort, slower in other tests
CCFLAGS=-c -Wall -Ofast -fopenmp -mfpmath=sse -std=gnu++11
// Slow sort, slower in other tests
CCFLAGS=-c -Wall -Ofast -fopenmp -mfpmath=sse
// Slow sort, fastest in other tests
CCFLAGS=-c -Wall -Ofast -fopenmp -mfpmath=sse -march=native
Run Code Online (Sandbox Code Playgroud)
结论似乎是相同的 - -std = gnu ++ 11大幅加速排序,几乎在其他任何地方都会受到轻微的惩罚.-march = native使用时加速程序.
鉴于每一代只调用一次,我将采用不使用-std = gnu ++ 11进行编译的速度优势,但我仍然对导致这些结果的原因非常感兴趣.
我正在使用#include提供的// std :: sort
人们对为什么排序方法的性能下降如此感兴趣。
我更感兴趣的是为什么剩余的代码看到了很好的改进,但为了帮助对话,下面是我的代码的唯一部分,在 -std=gnu++11 下速度更快
它只是向量对象成员上的双精度比较。
class TvectorPM {
public:
pthread_mutex_t lock;
std::vector<PopulationMember> v;
void add(PopulationMember p);
};
void TvectorPM::add(PopulationMember p) {
pthread_mutex_lock(&lock);
v.push_back(p);
pthread_mutex_unlock(&lock);
}
class PopulationManager {
public:
TvectorPM populationlist;
}
void PopulationManager::sortByScore() {
// Have overloaded operator< to make this fast
sort(populationlist.v.begin(),populationlist.v.end());
}
class PopulationMember {
public:
bool hasChanged;
double score;
inline bool operator< (const PopulationMember& rhs) const{
return this->score < rhs.score;
}
Run Code Online (Sandbox Code Playgroud)