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.
平台
CPU: i7-4790k
OS: Windows 10 WSL2
不知为什么,只有MINGW gcc和WSL2上的gcc能够完成矢量化,MSVC与clang均警告无法矢量化。
优化结果
sqrt()替换为std::sqrt()float的重载版本而不是double步骤:
float改为vector<float>-ffast-math -march=native选项启用AVX指令集#pragma omp simd避免pointer aliasing影响优化对于
step()其中的不变量为G*dt和eps*eps, 提取到全局变量后加快了速度float x = stars.mass[j] * Gdt / d2; stars.vx[i] += dx * x; stars.vy[i] += dy * x; stars.vz[i] += dz * x;有一定提升,但感觉不是很必要
结果&总结
最终结果:
1946ms -> 170ms(11.4倍)std::sqrt(), 进行代数化简、循环无关量提出,不使用STL容器