# \`TypeVar\` or \`eltype\`, which is faster?

**URL:** <https://discourse.julialang.org/t/typevar-or-eltype-which-is-faster/44113>\
**Category:** Performance\
**Tags:** question\
**Created:** [August 1, 2020, 11:47pm UTC](https://discourse.julialang.org/t/typevar-or-eltype-which-is-faster/44113 "2020-08-01T23:47:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [August 1, 2020, 11:47pm UTC](https://discourse.julialang.org/t/typevar-or-eltype-which-is-faster/44113/1 "2020-08-01T23:47:59Z")

</div>

Consider the following code

```julia
using Unitful

function f(arr::AbstractArray{T}) where {T}
    N = length(arr)
    return NTuple{N,T}(arr)
end

```

or

```julia
function g(arr::AbstractArray)
    N, T = length(arr), eltype(arr)
    return NTuple{N,T}(arr)
end

```

The first one has `$(Expr(:static_parameter, 1))` in its `@code_warntype`

```julia
julia> @code_warntype f([1u"m", 2u"GPa", 3.0, 4])
Variables
  #self#::Core.Compiler.Const(f, false)
  arr::Array{Quantity{Float64,D,U} where U where D,1}
  N::Int64

Body::Tuple
1 ─ (N = Main.length(arr))
│ %2 = Core.apply_type(Main.NTuple, N, $(Expr(:static_parameter, 1)))::Type{Tuple{Vararg{Quantity{Float64,D,U} where U where D,_A}}} where _A
│ %3 = (%2)(arr)::Tuple
└── return %3

```

while the second has `Core.Compiler.Const`

```julia
julia> @code_warntype g([1u"m", 2u"GPa", 3.0, 4])
Variables
  #self#::Core.Compiler.Const(g, false)
  arr::Array{Quantity{Float64,D,U} where U where D,1}
  N::Int64
  T::Type{Quantity{Float64,D,U} where U where D}

Body::Tuple
1 ─ %1 = Main.length(arr)::Int64
│ %2 = Main.eltype(arr)::Core.Compiler.Const(Quantity{Float64,D,U} where U where D, false)
│ (N = %1)
│ (T = %2)
│ %5 = Core.apply_type(Main.NTuple, N, T::Core.Compiler.Const(Quantity{Float64,D,U} where U where D, false))::Type{Tuple{Vararg{Quantity{Float64,D,U} where U where D,_A}}} where _A
│ %6 = (%5)(arr)::Tuple
└── return %6

```

Which one should be faster? What’s the difference? Which is more idiomatic?

```julia
julia> @btime f([1u"m", 2u"GPa", 3.0, 4])
  4.279 μs (48 allocations: 1.47 KiB)
(1.0 m, 2.0 GPa, 3.0, 4.0)

julia> @btime g([1u"m", 2u"GPa", 3.0, 4])
  4.296 μs (48 allocations: 1.47 KiB)
(1.0 m, 2.0 GPa, 3.0, 4.0)

julia> @btime f([1u"m", 2u"GPa", 3.0, 4])
  4.283 μs (48 allocations: 1.47 KiB)
(1.0 m, 2.0 GPa, 3.0, 4.0)

julia> @btime g([1u"m", 2u"GPa", 3.0, 4])
  4.303 μs (48 allocations: 1.47 KiB)
(1.0 m, 2.0 GPa, 3.0, 4.0)

julia> @btime $f([1u"m", 2u"GPa", 3.0, 4])
  4.312 μs (48 allocations: 1.47 KiB)
(1.0 m, 2.0 GPa, 3.0, 4.0)

julia> @btime $g([1u"m", 2u"GPa", 3.0, 4])
  4.302 μs (48 allocations: 1.47 KiB)
(1.0 m, 2.0 GPa, 3.0, 4.0)

julia> @btime $f([1u"m", 2u"GPa", 3.0, 4])
  4.306 μs (48 allocations: 1.47 KiB)
(1.0 m, 2.0 GPa, 3.0, 4.0)

julia> @btime $g([1u"m", 2u"GPa", 3.0, 4])
  4.273 μs (48 allocations: 1.47 KiB)
(1.0 m, 2.0 GPa, 3.0, 4.0

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [August 1, 2020, 11:50pm UTC](https://discourse.julialang.org/t/typevar-or-eltype-which-is-faster/44113/2 "2020-08-01T23:50:52Z")

</div>

They should always compile to essentially the same thing. That said, I feel like the first is slightly more clear.
