Hello !

I teach computational Physics in a french university. I learn to my students to code in Modern Fortran and Python. I know that many people claim that Julia may be as fast as C or Fortran. So, I try a little test : to compute pi with the Madhava-Leibniz formula. I wrote the same code in Fortran, Python and Julia.

Here are my codes :

Fortran :

program calcPiMadhava

use, intrinsic :: iso_fortran_env, only : wp => real64

implicit none

real(wp), parameter :: pi = 4.0_wp*atan(1.0_wp)

integer :: k = 0, nb

real(wp) :: S1 = 0.0_wp, S2 = 0.0_wp, eps

logical :: continuer = .True.

print *, "Number of significant digits expected ? "

read *, nb

eps = (0.1)**(nb+1)

do while (continuer)

S1 = S1 + 4.0_wp*((-1.0_wp)**k)/(2.0_wp*k + 1.0_wp)

if (abs(S1-S2) < eps) then

continuer = .False.

else

S2 = S1

k = k+1

end if

end do

print ‘("value of pi calculated with Madhava formula : ", f20.18)’, S1

print *, "pi = ", pi

print ‘("error = ", E40.30)’, abs(S1-pi)

print ‘("number of iterations : ", i10.1)’, k

end program calcPiMadhava

Python :

from numpy import pi

S1 = 0.0

S2 = 0.0

k = 0

continuer = True

nb = float(input(“Number of significant digits expected ?”))

eps = 0.1**(nb+1)

while continuer:

S1 = S1 + 4.0*((-1.0)**k)/(2.0*k + 1.0)

if abs(S1-S2) < eps:

continuer = False

else:

S2 = S1

k = k+1

print(f"value of pi calculated with Madhava formula : {S2}“)

print(f"Number of iterations : {k}”)

print(f"π = {pi}“)

print(f"error = {abs(S2-pi)}”)

Julia :

S1 = 0.0

S2 = 0.0

k = 0

continuer = true

print(“Number of significant digits expected ?”)

nb = parse(Int,readline())

eps = 0.1^(nb+1)

while continuer

global continuer, S1, S2, k, eps

S1 = S1 + 4.0*((-1.0)^k)/(2.0*k + 1.0)

if abs(S1-S2) < eps

continuer = false

else

S2 = S1

k = k+1

end

end

println("Value of pi calculated with Madhava formula : ", S2)

println("Number of iterations : ", k)

println("π = ", BigFloat(pi))

println("error = ", abs(S2-π))

If execute this 3 codes under Bash with the instructions time and ask 7 significant digits, it takes 17 s to Fortran to perform the calculation, 2.46 minutes to Python and 3.23 minutes to Julia.

So, Julia is the slowest …

I’m new to Julia, and my code is certainly perfectible, but I’d like to know how to obtain fast code in Julia.

At this time, I learn to my students to prototype codes under Python and translate them in Fortran if it is too slow. If Julia was as fast as Fortran, I would learn them Julia … And this is the reason of my question… Is Julia really fast for finite differences, FDTD, solving time dependent Schrodinger equation …

Many thanks in advance for your answers and help !

PE