79 Languages speed competition: Can we make Fortran win?

Here my results for the 4 diffent Fortran codes of the comparison project

The original, non tweaked algorithm has been presented (for Python, C# and C++) in What's the FASTEST Computer Language? C++ vs Fortran vs Cobol: E04 - YouTube

The rules are explained in Primes/CONTRIBUTING.md at drag-race · PlummersSoftwareLLC/Primes · GitHub

My non-faithful (it does not use classes) approach has been presented in the Fortran vs. Cobol comparison video, although there exists also faithful implementation by Thomas Jollans (github user tjol).
I do not know, I guess because it has a rather “classic” Fortran look.

The considered compilers are:
gfortran-7
gfortran-8
gfortran-9
gfortran-10
gfortran-11
ifort 21.04
ifx 21.04 beta
aocc 3.1.0 based on flang 12.0

The test have been performed on an AMD Ryzen 7 3700X CPU (base frequency 2200 Mhz,
max frequency 3600 MHz)

The numbers are the number of times the sieve algorithm is completed
for the range up to 1000000 with 5 seconds

Notable results are:

  • When a bitfiled is used, there are strong variations between different compilers
    (and different versions of gfortran). The variations increase for the object-oriented version

  • The fastest code is generated if one 1-bit integer is used to store the state (prime/not prime)
    of a number (gain ~50% over the best bitfield results). There the results are more consistent than for the bitfield

  • Storing the states of the numbers in an array of logical variables is slower than storing it in an 8bit integer and similar to the performance of a good bitfield implementation.

  • Disclaimer: I do not believe that I really chose the optimal command line options

solution_1 One bit per digit (stored in int64 variables), not object oriented, shown in video

gfortran-7 -march=native -O3 -o benchmark-gcc-7 PrimesFortran.f08
8905.2

gfortran-8 -march=native -O3 -o benchmark-gcc-8 PrimesFortran.f08
11210

gfortran-9 -march=native -O3 -o benchmark-gcc-9 PrimesFortran.f08
11211

gfortran-10 -march=native -O3 -o benchmark-gcc-10 PrimesFortran.f08
11323

gfortran-11 -march=native -O3 -o benchmark-gcc-11 PrimesFortran.f08
12417

Note: ifort -v results in ifort version 2021.4.0 ifort dors not recognize the .f08 suffix changed to .f90
ifort -Ofast -march=core-avx2 -o benchmark-ifort-21.04 PrimesFortran.f90
6097

ifx -Ofast -march=core-avx2 -o benchmark-ifx-21.04-beta PrimesFortran.f9
7283

flang -march=native -O3 -o benchmark-flang-12.0 PrimesFortran.f90
6659

Object-oriented with bit array, fully meets the ‘faithful’ criteria of the comparison

gfortran-7 -march=native -O3 -oo-bitarray-gcc-7 prime-bitarray.f90
12552

gfortran-8 -march=native -O3 -oo-bitarray-gcc-8 prime-bitarray.f90
8199

gfortran-9 -march=native -O3 -oo-bitarray-gcc-9 prime-bitarray.f90
8138

gfortran-10 -march=native -O3 -oo-bitarray-gcc-10 prime-bitarray.f90
7613

gfortran-11 -march=native -O3 -oo-bitarray-gcc-11 prime-bitarray.f90
12619

ifort -march=core-avx2 -O3 -oo-bitarray-ifort-12.04 prime-bitarray.f90
2591.0

ifx -march=core-avx2 -O3 -oo-bitarray-ifx-12.04 prime-bitarray.f90
2547

flang -march=native -O3 -oo-bitarray-flang-12.0 prime-bitarray.f90
1412

Object-oriented, one 8-bit Integer per number

gfortran-7 -march=native -O3 -oo-8bit-gcc-7 prime-8bit.f90
18155

gfortran-8 -march=native -O3 -oo-8bit-gcc-8 prime-8bit.f90
16597

gfortran-9 -march=native -O3 -oo-8bit-gcc-9 prime-8bit.f90
17870

gfortran-10 -march=native -O3 -oo-8bit-gcc-10 prime-8bit.f90
18029

gfortran-11 -march=native -O3 -oo-8bit-gcc-11 prime-8bit.f90
18247

ifort -march=core-avx2 -O3 -oo-8bit-ifort-12.4 prime-8bit.f90
18086

ifx -march=core-avx2 -O3 -oo-8bit-ifort-12.4 prime-8bit.f90
18361

flang -march=native -O3 -oo-8bit-flang-12.0 prime-8bit.f90
9651
(Note: The values varied strongly between approx- 4000 and approx. 11000)

Object-oriented, one Logical per number

gfortran-7 -march=native -O3 -oo-logical-gcc-7 prime-logical-array.f90
12833

gfortran-8 -march=native -O3 -oo-logical-gcc-8 prime-logical-array.f90
12764

gfortran-9 -march=native -O3 -oo-logical-gcc-9 prime-logical-array.f90
12441

gfortran-10 -march=native -O3 -oo-logical-gcc-10 prime-logical-array.f90
12453

gfortran-11 -march=native -O3 -oo-logical-gcc-11 prime-logical-array.f90
12635.8

ifort -march=core-avx2 -O3 -oo-logical-ifort-12.4 prime-logical-array.f90
13174

ifx -march=core-avx2 -O3 -oo-logical-ifort-12.4 prime-logical-array.f90
13385

flang -march=native -O3 -oo-logical-flang-12.0 prime-logical-array.f90
5573
(Note: strong fluctuations of the results between ~3800 and ~8300)

2 Likes