# Performance of log.(x)

**URL:** https://discourse.julialang.org/t/performance-of-log-x/23039
**Category:** General Usage
**Tags:** performance, broadcast
**Created:** [April 11, 2019, 2:26am UTC](https://discourse.julialang.org/t/performance-of-log-x/23039 "2019-04-11T02:26:05Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![pmags](https://avatars.discourse-cdn.com/v4/letter/p/49beb7/32.png) [@pmags](https://discourse.julialang.org/u/pmags)
#### Post date: [April 11, 2019, 2:26am UTC](https://discourse.julialang.org/t/performance-of-log-x/23039/1 "2019-04-11T02:26:05Z")

</div>

I’m a newcomer to Julia from Python and was surprised to find vectorized `log` so slow in Julia. Am I doing something wrong in the code below?

As noted [here](https://github.com/JuliaLang/julia/issues/8869#issuecomment-481935075) `numpy.log` seems to be about 6x faster than `Base.log` broadcast to an array.

```julia
function test_log()
    x = randn(10000) .+ 5
    @benchmark log.($x)
end

```

`@benchmark test_log()` yields:

```julia
BenchmarkTools.Trial: 
  memory estimate: 78.20 KiB
  allocs estimate: 2
  --------------
  minimum time: 49.861 μs (0.00% GC)
  median time: 80.124 μs (0.00% GC)
  mean time: 97.785 μs (12.32% GC)
  maximum time: 43.889 ms (99.65% GC)
  --------------
  samples: 10000
  evals/sample: 1

```

As compared to:

```python
x = np.random.randn(10000) + 5
%timeit np.log(x)

```

Which yields:

```julia
15 µs ± 116 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

```

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [April 11, 2019, 2:55am UTC](https://discourse.julialang.org/t/performance-of-log-x/23039/2 "2019-04-11T02:55:12Z")

</div>

Welcome!

My guess would be that you’re getting SIMD operations in numpy but not in Julia. Interestingly, I can’t reproduce your timing numbers; instead, my Julia timing roughly matches yours, but my numpy results are about 10X slower:

```julia
julia> x = randn(10000) .+ 5;

julia> @benchmark log.($x)
BenchmarkTools.Trial: 
  memory estimate: 78.20 KiB
  allocs estimate: 2
  --------------
  minimum time: 57.371 μs (0.00% GC)
  median time: 59.739 μs (0.00% GC)
  mean time: 67.735 μs (7.25% GC)
  maximum time: 35.763 ms (99.67% GC)
  --------------
  samples: 10000
  evals/sample: 1

```

```julia
In [3]: x = np.random.randn(10000) + 5

In [4]: %timeit np.log(x)
173 µs ± 5.53 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

```

There was a related discussion of SIMD optimizations for logs here: [Performance of logarithm calculation (when not to use the dot operator?) - #4 by kristoffer.carlsson](https://discourse.julialang.org/t/performance-of-logarithm-calculation-when-not-to-use-the-dot-operator/10504/4) although I’m afraid I don’t know enough about the details to be sure if it’s still relevant.

You also might want to consider making a separate thread for this discussion, as it’s only tangentially related to the topic of the original post (which was simply a question of syntax, not of performance optimization).

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [April 11, 2019, 4:28am UTC](https://discourse.julialang.org/t/performance-of-log-x/23039/3 "2019-04-11T04:28:16Z")

</div>

We explored different ways of making exp faster in this thread  
[https://discourse.julialang.org/t/fast-logsumexp/](https://discourse.julialang.org/t/fast-logsumexp/)  
The same tricks would apply to log

---

<div class="post-metadata">

### Author: ![pmags](https://avatars.discourse-cdn.com/v4/letter/p/49beb7/32.png) [@pmags](https://discourse.julialang.org/u/pmags)
#### Post date: [April 11, 2019, 1:32pm UTC](https://discourse.julialang.org/t/performance-of-log-x/23039/4 "2019-04-11T13:32:01Z")

</div>

@rdedit – Thanks for comparing your results. I’m guessing that the differences in our NumPy timings may relate to the fact that the NumPy I’m using is linked against MKL (this is the default for the Anaconda distributed versions of the NumPy/SciPy toolchain)?

---

<div class="post-metadata">

### Author: ![pmags](https://avatars.discourse-cdn.com/v4/letter/p/49beb7/32.png) [@pmags](https://discourse.julialang.org/u/pmags)
#### Post date: [April 11, 2019, 1:40pm UTC](https://discourse.julialang.org/t/performance-of-log-x/23039/5 "2019-04-11T13:40:00Z")

</div>

@baggepinnen – Thanks for the link to the related discussion. I gave Yeppp a shot but unfortunately `Yeppp.log` is about twice as slow on my platform/CPU as `Base.log`

```julia
julia> using Yeppp
julia> x = randn(10000 ) .+ 5;
julia> @benchmark Yeppp.log($x)
BenchmarkTools.Trial: 
  memory estimate: 78.20 KiB
  allocs estimate: 2
  --------------
  minimum time: 133.556 μs (0.00% GC)
  median time: 174.349 μs (0.00% GC)
  mean time: 193.267 μs (4.51% GC)
  maximum time: 3.532 ms (92.44% GC)
  --------------
  samples: 10000
  evals/sample: 1

```

For reference, here’s my Julia and system info:

```julia
julia> versioninfo()
Julia Version 1.1.0
Commit 80516ca202 (2019-01-21 21:24 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin14.5.0)
  CPU: Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, skylake)

```

---

<div class="post-metadata">

### Author: ![RoyiAvital](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/royiavital/32/571_2.png) [@RoyiAvital](https://discourse.julialang.org/u/RoyiAvital)
#### Post date: [April 11, 2019, 7:06pm UTC](https://discourse.julialang.org/t/performance-of-log-x/23039/6 "2019-04-11T19:06:34Z")

</div>

You can’t (Usually) beat Intel MKL on those kind of operations.

If it is already available, try Julia Pro Distribution in MKL Flavor.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [April 11, 2019, 7:49pm UTC](https://discourse.julialang.org/t/performance-of-log-x/23039/7 "2019-04-11T19:49:12Z")

</div>

> [@RoyiAvital](#):
>
> If it is already available, try Julia Pro Distribution in MKL Flavor.

I don’t think there is any support for different special function in JuliaPro.

---

<div class="post-metadata">

### Author: ![RoyiAvital](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/royiavital/32/571_2.png) [@RoyiAvital](https://discourse.julialang.org/u/RoyiAvital)
#### Post date: [April 11, 2019, 8:14pm UTC](https://discourse.julialang.org/t/performance-of-log-x/23039/8 "2019-04-11T20:14:27Z")

</div>

What do you mean?

Do you mean that even if MKL is integrated Julia won’t use it for this?

I’m really puzzled why, when MKL is integrated, it is not used fully.  
For instance, what about MKL Sparse capabilities?

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [April 11, 2019, 8:33pm UTC](https://discourse.julialang.org/t/performance-of-log-x/23039/9 "2019-04-11T20:33:42Z")

</div>

> [@RoyiAvital](#):
>
> What do you mean?
> 
> Do you mean that even if MKL is integrated Julia won’t use it for this?
> 
> I’m really puzzled why, when MKL is integrated, it is not used fully.

When built with MKL, Julia will use MKL’s BLAS, nothing else.

> [@RoyiAvital](#):
>
> For instance, what about MKL Sparse capabilities?

No, there is [GitHub - JuliaSparse/MKLSparse.jl: Make available to Julia the sparse functionality in MKL](https://github.com/JuliaSparse/MKLSparse.jl) and [GitHub - JuliaSparse/Pardiso.jl: Calling the PARDISO library from Julia](https://github.com/JuliaSparse/Pardiso.jl).

---

<div class="post-metadata">

### Author: ![improbable22](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/improbable22/32/5464_2.png) [@improbable22](https://discourse.julialang.org/u/improbable22)
#### Post date: [April 11, 2019, 9:07pm UTC](https://discourse.julialang.org/t/performance-of-log-x/23039/10 "2019-04-11T21:07:07Z")

</div>

If I understand right `log.(x)` will never be farmed out to MKL, but something like [https://github.com/rprechelt/Vectorize.jl](https://github.com/rprechelt/Vectorize.jl) would do this.

---

<div class="post-metadata">

### Author: ![pmags](https://avatars.discourse-cdn.com/v4/letter/p/49beb7/32.png) [@pmags](https://discourse.julialang.org/u/pmags)
#### Post date: [April 12, 2019, 1:50am UTC](https://discourse.julialang.org/t/performance-of-log-x/23039/11 "2019-04-12T01:50:16Z")

</div>

For those interested in this topic, please also see the parallel discussion here: [https://github.com/JuliaLang/julia/issues/8869](https://github.com/JuliaLang/julia/issues/8869)

---

<div class="post-metadata">

### Author: ![RoyiAvital](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/royiavital/32/571_2.png) [@RoyiAvital](https://discourse.julialang.org/u/RoyiAvital)
#### Post date: [April 12, 2019, 6:00pm UTC](https://discourse.julialang.org/t/performance-of-log-x/23039/12 "2019-04-12T18:00:50Z")

</div>

This is really nice progress.  
I really like to see better and better integration with MKL.  
Even if it is on side packages.

Do you have donation box for your MKL efforts?
