# Compile-time vs run-time

**URL:** <https://discourse.julialang.org/t/compile-time-vs-run-time/107606>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 14, 2023, 12:32pm UTC](https://discourse.julialang.org/t/compile-time-vs-run-time/107606 "2023-12-14T12:32:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jiang\_ming\_zhang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jiang_ming_zhang/32/204063_2.png) [@jiang\_ming\_zhang](https://discourse.julialang.org/u/jiang_ming_zhang)\
**Post date:** [December 14, 2023, 12:32pm UTC](https://discourse.julialang.org/t/compile-time-vs-run-time/107606/1 "2023-12-14T12:32:09Z")

</div>

I saw people use ‘Val’ in their codes.

```julia
help?> ntuple
search: ntuple NTuple NamedTuple @NamedTuple UnitUpperTriangular

  ntuple(f::Function, n::Integer)

  Create a tuple of length n, computing each element as f(i), where i is the index of the element.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> ntuple(i -> 2*i, 4)
  (2, 4, 6, 8)

  ────────────────────────────────────────────────────
  ntuple(f, ::Val{N})

  Create a tuple of length N, computing each element as f(i), where i is the index of the element. By taking a Val(N) argument, it is possible that this version of  
  ntuple may generate more efficient code than the version taking the length as an integer. But ntuple(f, N) is preferable to ntuple(f, Val(N)) in cases where N     
  cannot be determined at compile time.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> ntuple(i -> 2*i, Val(4))
  (2, 4, 6, 8)

```

I am not sure about the meaning of ’ But ntuple(f, N) is preferable to ntuple(f, Val(N)) in cases where N cannot be determined at compile time.’

what does it mean that a variable can be determined at compile time, or that it cannot be determined at compile time but only at run time?

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [December 14, 2023, 1:26pm UTC](https://discourse.julialang.org/t/compile-time-vs-run-time/107606/2 "2023-12-14T13:26:27Z")

</div>

At compile time, type information is known but not any information about values.

```julia-repl
julia> f(A::Array{T,N}) where {T,N}= ntuple(i->zero(T), Val(N))
f (generic function with 2 methods)

julia> f(rand(2,3))
(0.0, 0.0)

```

In `f` we know `T` and `N` at compile time since the information comes the `Array` type.

```julia-repl
julia> g(x::T) where T = ntuple(i->zero(eltype(T)), length(x))
g (generic function with 2 methods)

julia> g([1,2,4])
(0, 0, 0)

```

In `g` we do not know the length of `x` at compile time.

The reason this is important is dispatch. If we use `Val` we will dispatch to a specialized version of `ntuple` particular that value passed in. If it is runtime based, then we will end up compiling a new method of `ntuple` for every value.

Notice what `Val` does.

```julia
julia> Val(5)
Val{5}()

```

The `5` is in curly brackets, `{5}`, meaning it is a type parameter. `Val` moves information from the value domain to the type domain sowe can use it at compile time.
