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.
电脑硬件:
Intel(R) Xeon(R) CPU E5-2678 v3 @ 2.50GHz
final result:
6285 ms / 1096 ms = 5.734
优化过程如下:
原始版本
Initial energy: -13.414000
Final energy: -13.356842
Time elapsed: 6285 ms
-o1 优化
Initial energy: -13.414000
Final energy: -13.356842
Time elapsed: 2659 ms
-o2
Initial energy: -13.414000
Final energy: -13.356842
Time elapsed: 2558 ms
-o3
Initial energy: -13.414000
Final energy: -13.356842
Time elapsed: 2560 ms
索引int变成size_t
Initial energy: -13.414000
Final energy: -13.356842
Time elapsed: 2560 ms
inline frand函数
Initial energy: -13.414000
Final energy: -13.356842
Time elapsed: 2593 ms
将/2改成*0.5
Initial energy: -13.414000
Final energy: -13.356842
Time elapsed: 2585 ms
将公共除法变成乘法
Initial energy: -13.414000
Final energy: -13.356842
Time elapsed: 2030 ms
-ffast-math 选项定义了预处理器宏 FAST_MATH, 指示编译不必遵循 IEEE 和 ISO 的浮点运算标准
-march=native,GCC会自动检测你的CPU支持的指令集。
Initial energy: -13.414000
Final energy: -13.356841
Time elapsed: 1606 ms
SoA 阵列结构 struct of array
Initial energy: -13.414000
Final energy: -13.356841
Time elapsed: 1630 ms
sqrt前+std:: 使用模板完成传入参数匹配
Initial energy: -13.414000
Final energy: -13.356841
Time elapsed: 1220 ms
#pragma GCC unroll 4
Initial energy: -13.414000
Final energy: -13.356841
Time elapsed: 1212 ms
G * dt / d2 提取到循环外面计算
Initial energy: -13.414000
Final energy: -13.356841
Time elapsed: 1096 ms