# Performance of printing a type name

**URL:** <https://discourse.julialang.org/t/performance-of-printing-a-type-name/85468>\
**Category:** Performance\
**Created:** [August 8, 2022, 11:06am UTC](https://discourse.julialang.org/t/performance-of-printing-a-type-name/85468 "2022-08-08T11:06:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ikirill](https://avatars.discourse-cdn.com/v4/letter/i/43a26b/32.png) [@ikirill](https://discourse.julialang.org/u/ikirill)\
**Post date:** [August 8, 2022, 11:06am UTC](https://discourse.julialang.org/t/performance-of-printing-a-type-name/85468/1 "2022-08-08T11:06:08Z")

</div>

Is this expected, or is this a bug? Printing `Val(:hello_world)` is ~1000 times slower than printing `:hello_world`, and I imagine this affects things like printing stack traces with complicated types.

```julia
julia> @benchmark string(Val(:a))
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
 Range (min … max): 311.461 μs … 1.826 ms ┊ GC (min … max): 0.00% … 80.96%
 Time (median): 321.411 μs ┊ GC (median): 0.00%
 Time (mean ± σ): 323.074 μs ± 32.385 μs ┊ GC (mean ± σ): 0.22% ± 1.87%

                 ▁▃▄▆█▇▆▇▇▇▄▄▅▄▅▃▄▂▁
  ▂▂▂▂▂▃▃▄▄▄▅▅▅▆▆████████████████████▆▆▆▅▅▄▄▄▃▃▃▃▃▃▃▂▂▂▂▂▂▂▂▁▂ ▅
  311 μs Histogram: frequency by time 336 μs <

 Memory estimate: 26.73 KiB, allocs estimate: 25.

julia> @benchmark string(:a)
BenchmarkTools.Trial: 10000 samples with 998 evaluations.
 Range (min … max): 14.309 ns … 778.920 ns ┊ GC (min … max): 0.00% … 95.49%
 Time (median): 16.483 ns ┊ GC (median): 0.00%
 Time (mean ± σ): 17.907 ns ± 16.759 ns ┊ GC (mean ± σ): 2.03% ± 2.14%

        ▁ █
  ▂▁▂▂▃▆███▇▆▅▅▄▄▄▃▃▄▆▃▃▃▄▃▂▂▂▂▂▂▃▃▃▄▄▄▄▄▃▃▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂ ▃
  14.3 ns Histogram: frequency by time 25.6 ns <

 Memory estimate: 24 bytes, allocs estimate: 1.

```

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [August 8, 2022, 11:38am UTC](https://discourse.julialang.org/t/performance-of-printing-a-type-name/85468/2 "2022-08-08T11:38:43Z")

</div>

The output of `string(Val(:a))` is `"Val{:a}()"` and not “a”. So it generates a longer string than if you convert a symbol to a string.

The usage of value of described here [Types · The Julia Language](https://docs.julialang.org/en/v1/manual/types/#%22Value-types%22) . Typically you would use multiple dispatch to enforce to compile the function you use for each new value.  
For example:

```julia
tostring(::Val{x}) where {x} = string(x)

```

and then `tostring(Val(:a))` is basically resolved at compile time.

(Of course, for this simple conversation example, it is very likely that there is no need to use `Val`. As noted in the linked documentation.)

---

<div class="post-metadata">

**Author:** ![ikirill](https://avatars.discourse-cdn.com/v4/letter/i/43a26b/32.png) [@ikirill](https://discourse.julialang.org/u/ikirill)\
**Post date:** [August 8, 2022, 1:04pm UTC](https://discourse.julialang.org/t/performance-of-printing-a-type-name/85468/3 "2022-08-08T13:04:52Z")

</div>

I understand that, but the difference is between 16ns and 321,000ns. That’s much more than due to generating a slightly longer, though still quite short, string.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [August 8, 2022, 2:27pm UTC](https://discourse.julialang.org/t/performance-of-printing-a-type-name/85468/4 "2022-08-08T14:27:14Z")

</div>

`print(:a)` doesn’t have to generate a string at all — a `Symbol` in Julia is just an [interned string](https://en.wikipedia.org/wiki/String_interning), so Julia already has the text representation. Whereas for printing an arbitrary type it needs to do introspection.
