# Slow Newton method

**URL:** <https://discourse.julialang.org/t/slow-newton-method/4590>\
**Category:** General Usage\
**Tags:** performance\
**Created:** [July 1, 2017, 9:10pm UTC](https://discourse.julialang.org/t/slow-newton-method/4590 "2017-07-01T21:10:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![barche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barche/32/79_2.png) [@barche](https://discourse.julialang.org/u/barche)\
**Post date:** [July 1, 2017, 9:10pm UTC](https://discourse.julialang.org/t/slow-newton-method/4590/1 "2017-07-01T21:10:01Z")

</div>

The following code, based on @dpsanders 's [hands-on Julia example](https://github.com/dpsanders/hands_on_julia/blob/master/during_sessions/Fractale%20de%20Newton.ipynb) seems to allocate much more than necessary, and is a lot slower as a result:

```julia
newton(f, f′, x) = x - f(x) / f′(x)

function do_newton(g, g′, x0)
    x = x0

    tolerance = 1e-15
    while abs(g(x)) > tolerance
        x = newton(g, g′, x)
    end
    
    return x
end

h(z) = z^3 - 1
h′(z) = 3z^2

function newton_fractal(width, step)
    range = -width:step:width
    L = length(range)

    N = zeros(Complex{Float64}, L, L)
    
    for I in CartesianRange(size(N))
        a = range[I[1]]
        b = range[I[2]]

        z = b + a*im

        N[I] = do_newton(h, h′, z)

        # tolerance = 1e-15
        # while abs(h(z)-0) > tolerance
        # z = newton(h,h′,z)
        # end
        # N[I] = z
    end
    return N
end

M = newton_fractal(2.0,0.005)
@time M = newton_fractal(2.0,0.005)

```

Running the above code as-is results in:

```julia
1.877332 seconds (45.34 M allocations: 1.273 GiB, 1.57% gc time)

```

However, if I “inline” the do\_newton by uncommenting the commented lines, the picture is entirely different:

```julia
0.275165 seconds (86 allocations: 9.796 MiB)

```

And as I typed the “inline” above, I just realized that I could put `@inline` in front of the `do_newton` function, and then both versions magically perform the same. So actually it’s solved, but I struggled too long with this not to post it, and anyway it wouldn’t have been solved without typing out the text to explain the problem. Thanks for reading, and if anyone could explain why the difference is so huge that would also be interesting.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [July 2, 2017, 12:02am UTC](https://discourse.julialang.org/t/slow-newton-method/4590/2 "2017-07-02T00:02:49Z")

</div>

It’s the specialization heuristic for `::Function` arguments.
