# abs2(Float64) takes longer than abs2(Complex64)?

**URL:** <https://discourse.julialang.org/t/abs2-float64-takes-longer-than-abs2-complex64/1819>\
**Category:** General Usage\
**Tags:** performance\
**Created:** [February 1, 2017, 12:48pm UTC](https://discourse.julialang.org/t/abs2-float64-takes-longer-than-abs2-complex64/1819 "2017-02-01T12:48:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ndbecker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ndbecker/32/10829_2.png) [@ndbecker](https://discourse.julialang.org/u/ndbecker)\
**Post date:** [February 1, 2017, 12:48pm UTC](https://discourse.julialang.org/t/abs2-float64-takes-longer-than-abs2-complex64/1819/1 "2017-02-01T12:48:39Z")

</div>

```julia
function test1(u,N)
    for _ in 1:N
        abs2(u)
    end
end

u = ones(Complex64, 1000000)
v = ones(1000000)
@time test1(u, 1000)
@time test1(v, 1000)

```

```julia
julia> @time test1(u,1000)
  1.428069 seconds (4.00 k allocations: 3.725 GB, 5.69% gc time)

julia> @time test1(v,1000)
  2.382491 seconds (4.00 k allocations: 7.451 GB, 11.91% gc time)

```

Seems strange to me, why wouldn’t abs2(complex) take longer than abs2(float)? It’s more arithmetic work (and more memory access)

---

<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:** [February 1, 2017, 1:40pm UTC](https://discourse.julialang.org/t/abs2-float64-takes-longer-than-abs2-complex64/1819/2 "2017-02-01T13:40:02Z")

</div>

I see a much closer timing then yours. Note that `abs2(Complex64)` (i.e. `abs2(Complex{Float32})`) is not necessarily slower than `abs2(Float64)` especially since the operation is cheap comparing to the memory access anyway. Replacing `u` with `ones(Complex128, 1000000)` does make it consistently slower than `v` for me.

---

<div class="post-metadata">

**Author:** ![antoine-levitt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antoine-levitt/32/4008_2.png) [@antoine-levitt](https://discourse.julialang.org/u/antoine-levitt)\
**Post date:** [February 1, 2017, 1:58pm UTC](https://discourse.julialang.org/t/abs2-float64-takes-longer-than-abs2-complex64/1819/3 "2017-02-01T13:58:56Z")

</div>

```julia
julia> abs2(ones(Complex64,2))
2-element Array{Float32,1}:
 1.0
 1.0

```

So the output array is half the size in the first case (float32 vs float64), which is what you see in the allocations line. Since this code is memory-bound, that explains the timing difference.
