# Is it the correct way to check for specializations?

**URL:** <https://discourse.julialang.org/t/is-it-the-correct-way-to-check-for-specializations/93490>\
**Category:** General Usage\
**Created:** [January 25, 2023, 4:40am UTC](https://discourse.julialang.org/t/is-it-the-correct-way-to-check-for-specializations/93490 "2023-01-25T04:40:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Egwene\_al\_Vere](https://avatars.discourse-cdn.com/v4/letter/e/df788c/32.png) [@Egwene\_al\_Vere](https://discourse.julialang.org/u/Egwene_al_Vere)\
**Post date:** [January 25, 2023, 4:40am UTC](https://discourse.julialang.org/t/is-it-the-correct-way-to-check-for-specializations/93490/1 "2023-01-25T04:40:52Z")

</div>

Following the last paragraph at [docs](https://docs.julialang.org/en/v1/manual/performance-tips/#Be-aware-of-when-Julia-avoids-specializing), I want to check if julia is generating specialized version for some of my code. Testing against some built-in functions, first define

```julia
function get_sp(wfx)
    for mi in wfx.specializations
        if !isnothing(mi)
            println(mi)
            println("-"^9)
        end
    end
end

```

Then, running

```julia
exp(1)
exp(rand(3, 3))
exp(rand(Float32, 3, 3))
exp(rand(ComplexF64, 3, 3))

get_sp(@which exp(rand(3, 3)))

```

shows

```julia
MethodInstance for exp(::Matrix{Float64})
---------
MethodInstance for exp(::Matrix{ComplexF64})
---------
MethodInstance for exp(::Matrix{Float32})
---------

```

while

```julia
sin(1)
sin(1.2)
sin(0.3im)
get_sp(@which sin(.3im))

```

just gives

```julia
MethodInstance for sin(::ComplexF64)
---------

```

Is it then correct to say that, for `exp(m)` specialzied versions are generated for different types of matrices while no specialized version is generated for different number types to `sin(x)`?

Thanks!

---

<div class="post-metadata">

**Author:** ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)\
**Post date:** [January 25, 2023, 4:59am UTC](https://discourse.julialang.org/t/is-it-the-correct-way-to-check-for-specializations/93490/2 "2023-01-25T04:59:58Z")

</div>

which returns just one method, and gives the specializations of that one method. In this case, I think you want to check all methods of sin to see what specializations each of them had

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [January 25, 2023, 9:52am UTC](https://discourse.julialang.org/t/is-it-the-correct-way-to-check-for-specializations/93490/3 "2023-01-25T09:52:53Z")

</div>

Beware, though, that `which` can _create_ specializations. A fairly safe approach is to use the MethodAnalysis package:

```julia
julia> using MethodAnalysis

julia> methodinstances(sin)
Core.MethodInstance[]

julia> sin(3)
0.1411200080598672

julia> sin(3.5)
-0.35078322768961984

julia> methodinstances(sin)
2-element Vector{Core.MethodInstance}:
 MethodInstance for sin(::Float64)
 MethodInstance for sin(::Int64)

julia> @which sin(3)
sin(x::Real)
     @ Base.Math math.jl:1488

julia> @which sin(3.5)
sin(x::T) where T<:Union{Float32, Float64}
     @ Base.Math special/trig.jl:29

```

So you can see it aggregated the specializations across multiple methods, once those specializations were created.

If you start a fresh session and reverse the order, calling `@which` before the first invocation of `methodinstances`, you’ll see those specializations already exist—they were created by `@which`.

---

<div class="post-metadata">

**Author:** ![Egwene\_al\_Vere](https://avatars.discourse-cdn.com/v4/letter/e/df788c/32.png) [@Egwene\_al\_Vere](https://discourse.julialang.org/u/Egwene_al_Vere)\
**Post date:** [January 25, 2023, 1:44pm UTC](https://discourse.julialang.org/t/is-it-the-correct-way-to-check-for-specializations/93490/4 "2023-01-25T13:44:38Z")

</div>

Thanks for the package!

---

<div class="post-metadata">

**Author:** ![bjarthur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bjarthur/32/9638_2.png) [@bjarthur](https://discourse.julialang.org/u/bjarthur)\
**Post date:** [April 14, 2023, 5:49pm UTC](https://discourse.julialang.org/t/is-it-the-correct-way-to-check-for-specializations/93490/5 "2023-04-14T17:49:29Z")

</div>

if `which` is to be avoided when looking for specializations, then the [documentation](https://docs.julialang.org/en/v1.9.0-rc2/manual/performance-tips/#Be-aware-of-when-Julia-avoids-specializing) needs to be updated. see the last sentence in the section linked to above.
