Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
初始数据
Initial energy: -13.414000
Final energy: -13.356842
Time elapsed: 1758 ms
(other.mass * G * dt / d2) 把重复计算的部分括号起来,编译器会将其移动到循环外。这步减少了大量计算。也帮助矢量化
Initial energy: -13.414000
Final energy: -13.356842
Time elapsed: 1273 ms
sqrt使用std,std里的函数优化更好
Initial energy: -13.414000
Final energy: -13.356842
Time elapsed: 1131 ms
添加优化指令-O3 -ffast-math -march=native,-ffast-math 选项让 GCC 更大胆地尝试浮点运算的优化,-O3提升优化等级。
-march=native指示编译器生成针对当前编译代码的机器上运行的CPU架构进行优化的代码
Initial energy: -13.413999
Final energy: -13.380254
Time elapsed: 359 ms
但是优化后精度出现了问题
修改成SOA。把所有的变量都变成数组的形式,当计算的时候就可以实现simd
Initial energy: -13.413999
Final energy: -13.356841
Time elapsed: 123 ms
其他的优化比如AOSOA,循环展开,除法变乘法, constexpr等等基本上都没什么提升。估计编译器本身就已经有优化了吧。
总共只提升了10倍左右。