在 CentOS 上觉得 Fortran 程序运行速度慢,通常不是 Fortran 语言本身的问题,而是编译、系统环境、库或配置导致的。下面按常见原因系统说明。
gfortran test.f90
这样生成的是 调试级代码,速度可能慢 5–20 倍。
✅ 正确做法:
gfortran -O2 test.f90
# 或
gfortran -O3 -march=native test.f90
如果用了 Intel Fortran(ifort) 但:
-O2 / -O3反而可能比 gfortran 慢。
✅ 建议:
ifort -O3 -xHost test.f90
很多 CentOS 默认:
如果你做的是:
速度会非常慢。
✅ 检查:
ldd your_program | grep blas
✅ 解决:
yum install openblas-devel
gfortran test.f90 -lopenblas
或使用 Intel MKL(最快)
CentOS 默认可能:
powersave 模式cpupower frequency-info
cpupower frequency-set -g performance
如果你是多核 CPU,但:
-fopenmp只会用 1 个核心。
✅ 示例:
gfortran -O3 -fopenmp test.f90
Fortran 程序常见慢点:
write✅ 改进:
unformattedflushgfortran 4.8gfortran 9+✅ 可升级:
yum install devtoolset-9
scl enable devtoolset-9 bash
gfortran --version
| 情况 | 相对速度 |
|---|---|
| 默认编译 | ❌ 1× |
| -O2 | ✅ 5–10× |
| -O3 + OpenBLAS | ✅✅ 20×+ |
你可以对照:
-O2 / -O3如果你愿意,可以把:
发出来,我可以直接帮你定位“到底慢在哪”。