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.

limit
counted_work.f90
Replay: real traced execution (multi-file project)
program counted_work_demo
    implicit none
    integer :: values(5)
    integer :: limit
    integer :: i
    integer :: total
    integer :: operations

    values = [2, 4, 6, 8, 10]
    limit = 3
    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 = 2
    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 = 5
    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
  1. values ← [2, 4, 6, 8, 10]

    9values = [2, 4, 6, 8, 10]10limit = 3
    values this step[2, 4, 6, 8, 10]values
  2. limit ← 3

    9values = [2, 4, 6, 8, 10]10limit = 311total = 0
    values this step3limit
  3. total ← 0

    10limit = 311total = 012operations = 0
    values this step0total
  4. operations ← 0

    11total = 012operations = 013do i = 1, limit
    values this step0operations
  5. total ← 2

    13do i = 1, limit14    total = total + values(i)15    operations = operations + 1
    values this step0 2total1i2values(i)
  6. operations ← 1

    14    total = total + values(i)15    operations = operations + 116end do
    values this step0 1operations
  7. total ← 6

    13do i = 1, limit14    total = total + values(i)15    operations = operations + 1
    values this step2 6total2i4values(i)
  8. operations ← 2

    14    total = total + values(i)15    operations = operations + 116end do
    values this step1 2operations
  9. total ← 12

    13do i = 1, limit14    total = total + values(i)15    operations = operations + 1
    values this step6 12total3i6values(i)
  10. operations ← 3

    14    total = total + values(i)15    operations = operations + 116end do
    values this step2 3operations
  11. print '(I0, 1X, I0)', total, operations

    16    end do17    print '(I0, 1X, I0)', total, operations18end program counted_work_demo
    output12 3
    values this step12total3operations
  1. values ← [2, 4, 6, 8, 10]

    9values = [2, 4, 6, 8, 10]10limit = 2
    values this step[2, 4, 6, 8, 10]values
  2. limit ← 2

    9values = [2, 4, 6, 8, 10]10limit = 211total = 0
    values this step2limit
  3. total ← 0

    10limit = 211total = 012operations = 0
    values this step0total
  4. operations ← 0

    11total = 012operations = 013do i = 1, limit
    values this step0operations
  5. total ← 2

    13do i = 1, limit14    total = total + values(i)15    operations = operations + 1
    values this step0 2total1i2values(i)
  6. operations ← 1

    14    total = total + values(i)15    operations = operations + 116end do
    values this step0 1operations
  7. total ← 6

    13do i = 1, limit14    total = total + values(i)15    operations = operations + 1
    values this step2 6total2i4values(i)
  8. operations ← 2

    14    total = total + values(i)15    operations = operations + 116end do
    values this step1 2operations
  9. print '(I0, 1X, I0)', total, operations

    16    end do17    print '(I0, 1X, I0)', total, operations18end program counted_work_demo
    output6 2
    values this step6total2operations
  1. values ← [2, 4, 6, 8, 10]

    9values = [2, 4, 6, 8, 10]10limit = 5
    values this step[2, 4, 6, 8, 10]values
  2. limit ← 5

    9values = [2, 4, 6, 8, 10]10limit = 511total = 0
    values this step5limit
  3. total ← 0

    10limit = 511total = 012operations = 0
    values this step0total
  4. operations ← 0

    11total = 012operations = 013do i = 1, limit
    values this step0operations
  5. total ← 2

    13do i = 1, limit14    total = total + values(i)15    operations = operations + 1
    values this step0 2total1i2values(i)
  6. operations ← 1

    14    total = total + values(i)15    operations = operations + 116end do
    values this step0 1operations
  7. total ← 6

    13do i = 1, limit14    total = total + values(i)15    operations = operations + 1
    values this step2 6total2i4values(i)
  8. operations ← 2

    14    total = total + values(i)15    operations = operations + 116end do
    values this step1 2operations
  9. total ← 12

    13do i = 1, limit14    total = total + values(i)15    operations = operations + 1
    values this step6 12total3i6values(i)
  10. operations ← 3

    14    total = total + values(i)15    operations = operations + 116end do
    values this step2 3operations
  11. total ← 20

    13do i = 1, limit14    total = total + values(i)15    operations = operations + 1
    values this step12 20total4i8values(i)
  12. operations ← 4

    14    total = total + values(i)15    operations = operations + 116end do
    values this step3 4operations
  13. total ← 30

    13do i = 1, limit14    total = total + values(i)15    operations = operations + 1
    values this step20 30total5i10values(i)
  14. operations ← 5

    14    total = total + values(i)15    operations = operations + 116end do
    values this step4 5operations
  15. print '(I0, 1X, I0)', total, operations

    16    end do17    print '(I0, 1X, I0)', total, operations18end program counted_work_demo
    output30 5
    values this step30total5operations
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.