# Julia's loop scope behavior differs from Fortran?

**URL:** https://discourse.julialang.org/t/julias-loop-scope-behavior-differs-from-fortran/50275
**Category:** General Usage
**Created:** [November 17, 2020, 6:55am UTC](https://discourse.julialang.org/t/julias-loop-scope-behavior-differs-from-fortran/50275 "2020-11-17T06:55:06Z")
**Posts on this page:** 1
**Showing post:** 23

<div class="post-metadata">

### Author: ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)
#### Post date: [November 17, 2020, 4:09pm UTC](https://discourse.julialang.org/t/julias-loop-scope-behavior-differs-from-fortran/50275/23 "2020-11-17T16:09:32Z")

</div>

> [@lmiq](#):
>
> Yes, that is clear. Does that mean that `gfortran` is just too bad?

EDIT: I get the exact same performance between them (and `-march=native` doesn’t seem to matter).  
Compiling with

```sh
gfortran -O3 -march=native rootloop.f90 -shared -fPIC -o libloop.so
gfortran -Ofast -march=native rootloop.f90 -shared -fPIC -o libloopfast.so

```

Yields:

```julia
julia> using BenchmarkTools

julia> function loop(x,n)
         s = 0.
         for i in 1:n
           r = sqrt(i)
           s = s + x*r
         end
         s
       end
loop (generic function with 1 method)

julia> function loopfast(x,n)
         s = 0.
         @fastmath for i in 1:n
           r = sqrt(i)
           s = s + x*r
         end
         s
       end
loopfast (generic function with 1 method)

julia> floop(x,n) = ccall((:loop_,"libloop.so"),Float64,(Ref{Float64},Ref{Int64}),x,n)
floop (generic function with 1 method)

julia> floopfast(x,n) = ccall((:loop_,"libloopfast.so"),Float64,(Ref{Float64},Ref{Int64}),x,n)
floopfast (generic function with 1 method)

julia> x = rand() ; n = 10_000_000;

julia> @btime loop($x, $n)
  13.054 ms (0 allocations: 0 bytes)
7.112252395049944e9

julia> @btime floop($x, $n)
  13.054 ms (0 allocations: 0 bytes)
7.112252395049944e9

julia> @btime loopfast($x, $n)
  6.982 ms (0 allocations: 0 bytes)
7.112252395049599e9

julia> @btime floopfast($x, $n)
  6.982 ms (0 allocations: 0 bytes)
7.1122523950496235e9

```

---

_[View the full topic](https://discourse.julialang.org/t/julias-loop-scope-behavior-differs-from-fortran/50275)._
