Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slowdown with @fastmath and complex numbers #22898

Closed
KristofferC opened this issue Jul 21, 2017 · 2 comments
Closed

Slowdown with @fastmath and complex numbers #22898

KristofferC opened this issue Jul 21, 2017 · 2 comments
Labels
complex Complex numbers performance Must go faster

Comments

@KristofferC
Copy link
Member

KristofferC commented Jul 21, 2017

using BenchmarkTools 

function fractal(z, c, lod)
   for i in 1 : lod
          @fastmath (abs(z) > 2) ? (return i) : (z = z^2 + c) 
   end
   return lod
end
julia> @btime fractal(1im,1im,1)
  46.854 ns (2 allocations: 48 bytes)

Removing @fastmath:

julia> @btime fractal(1im,1im,1)
  9.455 ns (0 allocations: 0 bytes)

Ref: #19298

One problem is the type instability where z first is a Complex{Int64} and then turned into a Comlex{Float64} after the exponentiation. Fixing this removes the allocation but doesn't change the execution speed:

julia> @btime fractal(1.0im, 1.0im, 1)
  46.405 ns (0 allocations: 0 bytes)

Edit: It seems that the fast macro makes the exponent dispatch to a function that promotes both numbers to complex.

Edit2: Fixing the above, it turns out that the generic power by squaring is faster anyway up to an exponent of ~ 1000000000...

Edit3: The @fastmath macro also destroys the Val{2} exponent optimization in the lowering since the syntax is rewritten to pow_fast(z, 2).

@KristofferC KristofferC added complex Complex numbers performance Must go faster labels Jul 21, 2017
@ajkeller34
Copy link
Contributor

re: edit3: #21099 has been open for a while. It'd be nice to either merge or close that.

@KristofferC
Copy link
Member Author

Runs at same speed now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
complex Complex numbers performance Must go faster
Projects
None yet
Development

No branches or pull requests

2 participants