The code becomes about 10 times faster on my computer if I replace "bar=0" 
with "bar=0.0", 
see 
http://docs.julialang.org/en/release-0.4/manual/performance-tips/#avoid-changing-the-type-of-a-variable

(Also running "foo()" twice avoids measuring JIT compilation.)

On Wednesday, June 1, 2016 at 1:15:54 PM UTC+2, Mosè Giordano wrote:
>
> Hi all,
>
> I'm working on a Fortran77 code, but I'd much prefer to translate it to 
> Julia.  However, I've the impression that when working with large arrays 
> (of the order of tens of thousands elements) Julia is way slower than the 
> equivalent Fortran code.  See the following examples:
>
> Julia:
>
> function foo()
>     array1 = rand(70, 1000)
>     array2 = rand(70, 1000)
>     array3 = rand(2, 70, 20, 20)
>     bar = 0
>     @time for l = 1:1000, k = 1:20, j = 1:20, i = 1:70
>         bar = bar +
>             (array1[i, l] - array3[1, i, j, k])^2 +
>             (array2[i, l] - array3[2, i, j, k])^2
>     end
> end
> foo()
>
> Fortran77 (it uses some GNU extensions, so gfortran is required):
>
>       program main
>       implicit none
>       double precision array1(70, 1000), array2(70, 1000),
>      &    array3(2, 70, 20, 20), bar, start, finish
>       integer i, j, k, l
>       call srand(time())
> c     Initialize "array1" and "array2"
>       do j = 1, 1000
>         do i = 1, 70
>           array1(i, j) = rand()
>           array2(i, j) = rand()
>         enddo
>       enddo
> c     Initialize "array3"
>       do l = 1, 2
>         do k = 1, 70
>           do j = 1, 20
>             do i = 1, 20
>               array3(i, j, k, l) = rand()
>             enddo
>           enddo
>         enddo
>       enddo
> c     Do the calculations
>       bar = 0
>       call cpu_time(start)
>       do l = 1, 1000
>         do k = 1, 20
>           do j = 1, 20
>             do i = 1, 70
>               bar = bar +
>      &            (array1(i, l) - array3(1, i, j, k))**2 +
>      &            (array2(i, l) - array3(2, i, j, k))**2
>             enddo
>           enddo
>         enddo
>       enddo
>       call cpu_time(finish)
>       print "(f10.6, a)", finish - start, " seconds"
>       end program main
>
> This is the result of running the two programs on my computer:
>
> % julia --version
> julia version 0.4.5
> % gfortran --version
> GNU Fortran (Debian 5.3.1-20) 5.3.1 20160519
> Copyright (C) 2015 Free Software Foundation, Inc.
>
> GNU Fortran comes with NO WARRANTY, to the extent permitted by law.
> You may redistribute copies of GNU Fortran
> under the terms of the GNU General Public License.
> For more information about these matters, see the file named COPYING
>
> % julia -f test.jl
>   1.099910 seconds (84.00 M allocations: 1.252 GB, 7.14% gc time)
> % gfortran test.f&&./a.out
>   0.132000 seconds
>
> While Julia code is 3 times shorter than the Fortran77 code (and this one 
> of the many reasons why I like Julia very much), it's also more than 8 
> times slower and my build of Julia 0.5 (updated to aa1ce87) performs even 
> worse, it takes about 1.4 seconds.  If I remove access to the arrays (just 
> put "bar = bar" as loop body) then Julia is infinitely faster than Fortran 
> in the sense that Julia takes 0.000000 seconds, Fortran 0.056000.
>
> Is this difference to be expected or are there tricks to fasten the Julia 
> code?  I should have gotten the order of nested loops right.  @inbounds 
> doesn't help that much.  Parallelization is probably not an option because 
> the code above is a small part of a larger loop that does data movement 
> (can DistributedArrays help?).
>
> Bye,
> Mosè
>

Reply via email to