nadavhalahmi
nadavhalahmi

Reputation: 101

random_number subroutine run time comparison ifort vs gfortran

I wrote this code:

program random
    implicit none

    integer :: i, j, limit
    real(8) :: r, max_val
    real(8) :: start, finish

    max_val = 0.d0
    limit = 10000

    call CPU_TIME(start)
    do i=1, limit
        do j=1, limit
            call random_number(r)
            max_val = max(max_val, r)
        end do
    end do
    call CPU_TIME(finish)
    print *, max_val
    print '("Time = ",f6.3," seconds.")',finish-start
end program random

And I compiled it with gfortran 10.1.0 and ifort 19.1.3.304 on CentOS Linux 7 using:

ifort *.f90 -O3 -no-vec -o intel.out

gfortran *.f90 -O3 -fno-tree-vectorize -o gnu.out

and the outputs are:

gnu:

0.9999999155521957
Time = 0.928 seconds.

intel:

0.999999968800691 (same for every run btw)
Time = 1.989 seconds.

When I run a few times, the run time of each is pretty much the same.

Why is gfortran faster than ifort and how can I make ifort run as fast as gfortran?

Upvotes: 0

Views: 231

Answers (1)

Different compilers have their libraries with implementations of their intrinsic functions and subroutines. They will differ in performance and may also differ in their results. Gfortran uses the GLIBC library for many general intrinsics and the libgfortran library for many Fortran-spific ones. The Intel compiler comes with its own runtime-library suite.

Notably, the Fortran standard gives no guarantees about the quality of the pseudo-random generator used for random_number(). Even if it did, the actual implementation in code could always differ and hence the actual performance.

There are many external pseudo-random number generator libraries available. Some faster, some slower. Some more robust, some fail certain randomness tests. (sometimes that does matter, sometimes it does not). Some give more random bits in a single call, some give fewer random bits in a single call. If you need some particular properties for the generator in all your compilers, you might be better off with an external library.

Upvotes: 2

Related Questions