Skip to content

Commit

Permalink
fuse broadcasted float and complex operations
Browse files Browse the repository at this point in the history
`float.(complex(v))` is less performant than `float.(complex.(v))`.

Relevant docs:
https://docs.julialang.org/en/v1/manual/performance-tips/#More-dots:-Fuse-vectorized-operations

Relevant microbenchmarks (nightly Julia):
```
julia> f(v) = float.(complex(v))
f (generic function with 1 method)

julia> g(v) = float.(complex.(v))
g (generic function with 1 method)

julia> h(v) = map(float ∘ complex, v)
h (generic function with 1 method)

julia> setprecision(2^11)
2048

julia> using BenchmarkTools

julia> f(BigFloat[1, 2, 3])
3-element Vector{Complex{BigFloat}}:
 1.0 + 0.0im
 2.0 + 0.0im
 3.0 + 0.0im

julia> g(BigFloat[1, 2, 3])
3-element Vector{Complex{BigFloat}}:
 1.0 + 0.0im
 2.0 + 0.0im
 3.0 + 0.0im

julia> h(BigFloat[1, 2, 3])
3-element Vector{Complex{BigFloat}}:
 1.0 + 0.0im
 2.0 + 0.0im
 3.0 + 0.0im

julia> @benchmark f(v) setup=(v = rand(BigFloat, 2^16);)
BenchmarkTools.Trial: 223 samples with 1 evaluation.
 Range (min … max):  4.225 ms … 39.270 ms  ┊ GC (min … max):  0.00% … 74.99%
 Time  (median):     4.474 ms              ┊ GC (median):     0.00%
 Time  (mean ± σ):   7.535 ms ±  5.772 ms  ┊ GC (mean ± σ):  30.20% ± 27.14%

  █▂                ▃                                ▃
  ██▅▄▁▁▁▁▁▁▁▁▁▁▁▁▁▆█▄▁▁▁▁▁▁▁▁▁▁▁▄▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁█▆▁▁▁▁▄ ▅
  4.23 ms      Histogram: log(frequency) by time     21.3 ms <

 Memory estimate: 23.00 MiB, allocs estimate: 131080.

julia> @benchmark g(v) setup=(v = rand(BigFloat, 2^16);)
BenchmarkTools.Trial: 223 samples with 1 evaluation.
 Range (min … max):  3.882 ms … 15.182 ms  ┊ GC (min … max):  0.00% … 72.54%
 Time  (median):     4.101 ms              ┊ GC (median):     0.00%
 Time  (mean ± σ):   7.448 ms ±  4.387 ms  ┊ GC (mean ± σ):  44.37% ± 32.61%

  █▅
  ██▄▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▅▁▁▁▁▁▁▁▁▄▁▁▅▆▇▆▇▇▆▆▆▅▇▆▇▆▅▇▇▇▅▇▇▇█ ▅
  3.88 ms      Histogram: log(frequency) by time     14.9 ms <

 Memory estimate: 22.00 MiB, allocs estimate: 131074.

julia> @benchmark h(v) setup=(v = rand(BigFloat, 2^16);)
BenchmarkTools.Trial: 213 samples with 1 evaluation.
 Range (min … max):  3.832 ms … 20.391 ms  ┊ GC (min … max):  0.00% … 79.66%
 Time  (median):     4.118 ms              ┊ GC (median):     0.00%
 Time  (mean ± σ):   8.597 ms ±  5.860 ms  ┊ GC (mean ± σ):  51.76% ± 35.27%

  █▄
  ██▅▄▁▁▁▁▁▁▁▁▁▁▁▁▄▄▁▁▁▁▁▄▇▆▆▅▅▅▆▅▅▅▄▄▅▆▆▄▆▅▅▅▄▇▆▅▅▆▅▆▅▅▆▆▅▇ ▅
  3.83 ms      Histogram: log(frequency) by time     19.9 ms <

 Memory estimate: 22.00 MiB, allocs estimate: 131074.
```
  • Loading branch information
nsajko committed Oct 12, 2022
1 parent de52131 commit 77db70d
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/PolynomialRoots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ function roots(poly::AbstractVector{<:Number}, roots::AbstractVector{<:Number};
epsilon::AbstractFloat=NaN, polish::Bool=false)
degree = length(poly) - 1
@assert degree == length(roots) "`poly' must have one element more than `roots'"
roots!(promote(float.(complex(roots)), float.(complex(poly)))...,
roots!(promote(float.(complex.(roots)), float.(complex.(poly)))...,
epsilon, degree, polish)
end

Expand Down Expand Up @@ -731,13 +731,13 @@ function roots5(poly::AbstractVector{<:Number}, roots::AbstractVector{<:Number};
epsilon::AbstractFloat=NaN)
@assert length(poly) == 6 "Use `roots' function for polynomials of degree != 5"
@assert length(roots) == 5 "`roots' vector must have 5 elements"
return roots5!(promote(float.(complex(roots)), float.(complex(poly)))...,
return roots5!(promote(float.(complex.(roots)), float.(complex.(poly)))...,
epsilon, true)
end

function roots5(poly::AbstractVector{N}; epsilon::AbstractFloat=NaN) where {N<:Number}
@assert length(poly) == 6 "Use `roots' function for polynomials of degree != 5"
return roots5!(zeros(Complex{real(float(N))}, 5), float.(complex(poly)),
return roots5!(zeros(Complex{real(float(N))}, 5), float.(complex.(poly)),
epsilon, false)
end

Expand Down

0 comments on commit 77db70d

Please sign in to comment.