# Slow complex exponential evaluation

**URL:** <https://discourse.julialang.org/t/slow-complex-exponential-evaluation/3170>\
**Category:** Numerics\
**Created:** [April 11, 2017, 2:54pm UTC](https://discourse.julialang.org/t/slow-complex-exponential-evaluation/3170 "2017-04-11T14:54:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![pkairys](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pkairys/32/2495_2.png) [@pkairys](https://discourse.julialang.org/u/pkairys)\
**Post date:** [April 11, 2017, 2:54pm UTC](https://discourse.julialang.org/t/slow-complex-exponential-evaluation/3170/1 "2017-04-11T14:54:09Z")

</div>

I am having some issues with code and I can’t seem to reach a solution. Specifically I am re-writing some Matlab code and for one or two things I can’t seem to match the speed of Matlab.

Matlab Code:

```julia
a=[0:2^24-1]
b=exp(2*pi*a*i)

```

This is pretty fast but I can’t seem to match the execution in Julia:

```julia
a=collect(0:24-1)
b=exp(2*pi*im*a)

```

This is very, very slow ~10s after compilation. I tried to make an explicit loop and attempt that and only saw negligible improvement:

```julia
function imexp_1d(x::Array)

        out::Array{Complex{Float64}}=Array(Complex{Float64},size(x))
        for i in eachindex(x)
                out[i]=cos(x[i]*2.0*pi)+sin(x[i]*2.0*pi)*im
                #out[i]=complex(cos(x[i]*2.0*pi),sin(x[i]*2.0*pi))
        end
        return out
end
tt=collect(Float64,0:2^24-1)
@time imexp_1d(tt);
@time imexp_1d(tt);

```

The output is still ~9s. Am I missing something? Any help would be appreciated.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [April 11, 2017, 3:04pm UTC](https://discourse.julialang.org/t/slow-complex-exponential-evaluation/3170/2 "2017-04-11T15:04:09Z")

</div>

> [@pkairys](#):
>
> out::Array{Complex{Float64}}=Array(Complex{Float64},size(x))

that’s not strictly typed. An `Array` has two paramters: the element type and the dimension. Instead, do

```julia
out=similar(x,Complex{Float64})

```

There’s no reason to declare the type either.

## Edit

Nevermind, I never knew you could do that with the `Array` constructor. Huh, that’s cool. I still use `similar`.

---

<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:** [April 11, 2017, 3:04pm UTC](https://discourse.julialang.org/t/slow-complex-exponential-evaluation/3170/3 "2017-04-11T15:04:12Z")

</div>

This is from matlab executing the operation with multiple threads.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [April 11, 2017, 3:09pm UTC](https://discourse.julialang.org/t/slow-complex-exponential-evaluation/3170/4 "2017-04-11T15:09:49Z")

</div>

For reference, here’s how I would do it:

```julia
function imexp{T}(x::AbstractArray{T})
  out = similar(x,Complex{T})
  imexp!(out,x)
  out
end
function imexp!(out,x::AbstractArray)
  for i in eachindex(x)
    out[i]=cos(x[i]*2pi)+sin(x[i]*2pi)*im
  end
end
tt=collect(Float64,0:2^24-1)
@time imexp(tt);
@time imexp(tt);

```

I get:

```julia
5.993910 seconds (6 allocations: 256.000 MB, 1.38% gc time)
5.971867 seconds (6 allocations: 256.000 MB, 1.15% gc time)

```

If you want threads, just do

```julia
function imexp!(out,x::AbstractArray)
  Theads.@threads for i in eachindex(x)
    out[i]=cos(x[i]*2.0*pi)+sin(x[i]*2.0*pi)*im
  end
end

```

and make sure you enabled multithreading:

[https://docs.julialang.org/en/stable/manual/parallel-computing/#setup](https://docs.julialang.org/en/stable/manual/parallel-computing/#setup)

## Edit

I realized that the `Array` constructor usage you did there actually works, so there’s no real timing difference. So it must all be multithreading.

---

<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:** [April 11, 2017, 3:11pm UTC](https://discourse.julialang.org/t/slow-complex-exponential-evaluation/3170/5 "2017-04-11T15:11:02Z")

</div>

> [@pkairys](#):
>
> a=collect(0:24-1)

Also I assume you mean `2^24`? It’ll be very surprising if doing `exp(::Complex128)` on 24 elements takes 10s for any machine we support…

---

<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:** [April 11, 2017, 3:16pm UTC](https://discourse.julialang.org/t/slow-complex-exponential-evaluation/3170/6 "2017-04-11T15:16:37Z")

</div>

> [@ChrisRackauckas](#):
>
> cos(x[i]_2.0_pi)+sin(x[i]_2.0_pi)\*im

`cis`

---

<div class="post-metadata">

**Author:** ![pkairys](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pkairys/32/2495_2.png) [@pkairys](https://discourse.julialang.org/u/pkairys)\
**Post date:** [April 11, 2017, 3:32pm UTC](https://discourse.julialang.org/t/slow-complex-exponential-evaluation/3170/7 "2017-04-11T15:32:24Z")

</div>

Thanks! Yea i guess it must be the multithreading. I just tried your modified code with 4 threads going and i got ~2.5s timing which is much better. I thought it might be the multithreading thing but when I looked it up Matlab’s docs said that they only used multhreading automatically for LinAlg type operations.

Also, is there a particular reason for using the imexp!() separately? Does it have to do with type-stability?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [April 11, 2017, 3:39pm UTC](https://discourse.julialang.org/t/slow-complex-exponential-evaluation/3170/8 "2017-04-11T15:39:02Z")

</div>

> [@pkairys](#):
>
> Also, is there a particular reason for using the imexp!() separately? Does it have to do with type-stability?

That’s an in-place operation. If your output array is already allocated, it can be quicker depending on the size of the array, and also it can put less of a load in the garbage collector if you’re using this in a loop.

---

<div class="post-metadata">

**Author:** ![pkairys](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pkairys/32/2495_2.png) [@pkairys](https://discourse.julialang.org/u/pkairys)\
**Post date:** [April 11, 2017, 3:45pm UTC](https://discourse.julialang.org/t/slow-complex-exponential-evaluation/3170/9 "2017-04-11T15:45:20Z")

</div>

Gotcha! Thanks a bunch.
