Performance

Contents
-O levels
Memory layout first
Whole-array wins
Measure with gprof concept

-O levels

Optimization levels trade compile time for run speed. Level zero means no optimization and fast builds, level two is a balanced default for release runs.

Start development with no optimization and checks on, then measure again with -O2 for speed. Extra architecture flags stay as text in build notes until needed.

gfortran -O2 main.f90 -o main_fast

main_fast runs the same results with less time

Memory layout first

Fortran stores arrays in column major order, so elements with neighboring first indices sit next to each other in memory.

Loop over the first index in the inner loop. This walks memory in order and keeps the cache busy with useful data.

program layout_demo implicit none integer, parameter :: n = 4 real :: a(n, n) integer :: i integer :: j do j = 1, n do i = 1, n a(i, j) = real(i + j) end do end do print *, a(1, 1), a(n, n) end program layout_demo

Whole-array wins

Whole-array statements express math on all elements at once and let the compiler pick a fast loop.

Prefer array assignment and intrinsics like sum and matmul for clear code before writing manual loops.

program array_demo implicit none real :: b(4) real :: c(4) real :: a(4) b = [1.0, 2.0, 3.0, 4.0] c = [5.0, 6.0, 7.0, 8.0] a = b + c print *, a, sum(a) end program array_demo

Measure with gprof concept

Profiling shows where time really goes. The classic gprof idea is simple: build with profiling marks, run a typical workload, then read the flat report.

Optimize only the top routine from the report. Guessing without numbers usually tunes code that hardly matters.

gfortran -pg main.f90 -o main_prof

run workload, then read gmon report for hot routines

Next: Modern Fortran tour: 2008, 2018, 2023

Article author: Arthur Isaev

Related articles
Fortran
Fortran course: zero to hero in 50 lessons
What is Fortran and where it runs
Install gfortran
Your first program
Program structure
Types: integer real complex logical character
Kind parameters
Operators
Conditions
DO loops
Arrays
Array sections
Character strings
Formatted IO
Files
Subroutines and functions
Modules
USE with ONLY
INTENT
OPTIONAL and PRESENT
Explicit interfaces
Derived types
Type-bound procedures
Object orientation
Operator overloading
Finalization
Allocatable arrays
Pointers in Fortran
DO CONCURRENT
Coarrays
OpenMP basics
Namelists
IEEE arithmetic
Intrinsic matrix math
Random numbers
String tools
Command-line arguments
Environment variables
error stop
Testing
Build with gfortran and make
Debugging
C interoperability
From fixed form to free form
Numeric models
Performance
Modern Fortran tour: 2008, 2018, 2023
Capstone: stats tool
Capstone: 1D heat equation
Style guide
Hero roadmap: the whole course on one page
Install gfortran on Windows 11
Install gfortran on Ubuntu
Install gfortran on Rocky Linux
Install gfortran on macOS
Install gfortran on FreeBSD
Fortran in Docker
Banner image

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: