Performance-Aware Loops
Counted Work
Bound the Loop
Performance-aware code starts by making the amount of loop work explicit, before any timing or tuning claims.
Program
Play the program to choose how many values are included and watch the operation count stay visible.
counted_work.f90
program counted_work_demo
implicit none
integer :: values(5)
integer :: limit
integer :: i
integer :: total
integer :: operations
values = [2, 4, 6, 8, 10]
limit =
total = 0
operations = 0
do i = 1, limit
total = total + values(i)
operations = operations + 1
end do
print '(I0, 1X, I0)', total, operations
end program counted_work_demo
program counted_work_demo
implicit none
integer :: values(5)
integer :: limit
integer :: i
integer :: total
integer :: operations
values = [2, 4, 6, 8, 10]
limit =
total = 0
operations = 0
do i = 1, limit
total = total + values(i)
operations = operations + 1
end do
print '(I0, 1X, I0)', total, operations
end program counted_work_demo
program counted_work_demo
implicit none
integer :: values(5)
integer :: limit
integer :: i
integer :: total
integer :: operations
values = [2, 4, 6, 8, 10]
limit =
total = 0
operations = 0
do i = 1, limit
total = total + values(i)
operations = operations + 1
end do
print '(I0, 1X, I0)', total, operations
end program counted_work_demo
work bound
`limit` makes the loop bound visible and easy to reason about.
operation count
`operations` records how many iterations actually ran.
work shape
Counting work shows the performance shape without measuring elapsed time.