# Detect if constant

**URL:** <https://discourse.julialang.org/t/detect-if-constant/100246>\
**Category:** General Usage\
**Tags:** constant-propagation\
**Created:** [June 12, 2023, 7:56pm UTC](https://discourse.julialang.org/t/detect-if-constant/100246 "2023-06-12T19:56:52Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![user664303](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/user664303/32/37843_2.png) [@user664303](https://discourse.julialang.org/u/user664303)\
**Post date:** [June 15, 2023, 10:35am UTC](https://discourse.julialang.org/t/detect-if-constant/100246/7 "2023-06-15T10:35:02Z")

</div>

This code and result demonstrates why being able to branch on whether something is known to be a constant is of such value:

> **Code**
>
> ```julia
> using StaticArrays, BenchmarkTools, Plots
> 
> function myfunc(::Val{n}) where n
> m = 2 ^ n
> V = rand(SVector{m})
> return V' * V
> end
> 
> function myfunc(n)
> m = 2 ^ n
> V = rand(m)
> return V' * V
> end
> 
> const topend = 9
> function timeit()
> times = zeros(topend, 3)
> times[1, 1] = @belapsed myfunc(Val(1))
> times[2, 1] = @belapsed myfunc(Val(2))
> times[3, 1] = @belapsed myfunc(Val(3))
> times[4, 1] = @belapsed myfunc(Val(4))
> times[5, 1] = @belapsed myfunc(Val(5))
> times[6, 1] = @belapsed myfunc(Val(6))
> times[7, 1] = @belapsed myfunc(Val(7))
> times[8, 1] = @belapsed myfunc(Val(8))
> times[9, 1] = @belapsed myfunc(Val(9))
> for a = 1:topend
> times[a, 2] = @belapsed myfunc(Val($a))
> times[a, 3] = @belapsed myfunc($a)
> end
> return times
> end
> 
> times = timeit()
> plot(2 .^ (1:topend), times .* 10e6, xlabel="Length of vector", ylabel="Function time (ms)", label=["SVector w. static dispatch" "SVector w. dynamic dispatch" "Vector"], xscale=:log10, yscale=:log10)
> 
> ```

 ![Graph of dot product timings](https://global.discourse-cdn.com/julialang/original/3X/8/5/8523be18fc6a5f8458e3e824b41eaa04a55bfb4e.png)

What it shows is that if I know that vector length is constant, I can get significant speedups by using SVector instead of Vector, up to a certain length. However, if the vector length is not constant, then the penalty incurred by dynamic dispatch negates all speed advantages of SVector, making it slower than Vector for all lengths.

It is not currently possible to exploit this fact to speed up some code without also risking slowing down the same code under different conditions, because, from what has been said here, I understand it isn’t possible to detect if a value is constant at runtime. This looks like it would be a useful feature.

---

_[View the full topic](https://discourse.julialang.org/t/detect-if-constant/100246)._
