# Why is BLAS dot product so much faster than Julia loop?

**URL:** https://discourse.julialang.org/t/why-is-blas-dot-product-so-much-faster-than-julia-loop/44994
**Category:** Performance
**Created:** [August 15, 2020, 2:53pm UTC](https://discourse.julialang.org/t/why-is-blas-dot-product-so-much-faster-than-julia-loop/44994 "2020-08-15T14:53:01Z")
**Posts on this page:** 1
**Showing post:** 15

<div class="post-metadata">

### Author: ![onButtonUp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/onbuttonup/32/3098_2.png) [@onButtonUp](https://discourse.julialang.org/u/onButtonUp)
#### Post date: [August 15, 2020, 3:38pm UTC](https://discourse.julialang.org/t/why-is-blas-dot-product-so-much-faster-than-julia-loop/44994/15 "2020-08-15T15:38:38Z")

</div>

> [@How to get number of current BLAS threads?](https://discourse.julialang.org/t/how-to-get-number-of-current-blas-threads/32090):
>
> LinearAlgebra.BLAS.set\_num\_threads() sets the number of threads. But, how to get the number of threads (so that I could save it, set another one, then restore the value)? Thanks.

cut and paste worked here . . .

```julia
const get_num_threads = function() # anonymous so it will be serialized when called
    blas = LinearAlgebra.BLAS.vendor()
    # Wrap in a try to catch unsupported blas versions
    try
        if blas == :openblas
            return ccall((:openblas_get_num_threads, Base.libblas_name), Cint, ())
        elseif blas == :openblas64
            return ccall((:openblas_get_num_threads64_, Base.libblas_name), Cint, ())
        elseif blas == :mkl
            return ccall((:MKL_Get_Max_Num_Threads, Base.libblas_name), Cint, ())
        end

        # OSX BLAS looks at an environment variable
        if Sys.isapple()
            return tryparse(Cint, get(ENV, "VECLIB_MAXIMUM_THREADS", "1"))
        end
    catch
    end

    return nothing
end

julϊ̇a> using LinearAlgebra

julϊ̇a> get_num_threads()
4

```

---

_[View the full topic](https://discourse.julialang.org/t/why-is-blas-dot-product-so-much-faster-than-julia-loop/44994)._
