I read in several places that the performance of Julia code can (under certain conditions) be compared to the one of Fortran. I wrote the following code in Julia:
Pi = 3.141592653589793238462643
n = 100000
function integration_2d(n,Pi,sum)
h = Pi/n
for i=1:n
x = h*(i-0.5)
for j=1:n
y = h*(j-0.5)
sum = sum + cos(x + y)
end
end
sum*h*h
end
and the mean execution time was 180 sec. A Fortran code which has a very close structure than the one in Fortran compiled with -O3
option had an execution time of 0.013 sec. I wonder where the Julia code is losing performance, any comment is appreciated. Thanks.
cos
a total number of 100000^2 = 10^10 times. You claim that in Fortran this takes 0.013 seconds. This means that each cosine evaluation takes 1.3*10^(-12) seconds. A CPU can do very approximately one operation per nanosecond 10^(-9) seconds. So clearly, the Fortran code is not doing the work you think it is doing at runtime. This is a constant danger with benchmarking. you have to make sure that you are measuring what you think you are measuring. - Kristoffer CarlssonPi
, sincepi
is already a built-in constant in Julia. - DNF