# Why is converting from a type to a symbol so slow?

**URL:** <https://discourse.julialang.org/t/why-is-converting-from-a-type-to-a-symbol-so-slow/103212>\
**Category:** Performance\
**Tags:** question\
**Created:** [August 25, 2023, 10:32pm UTC](https://discourse.julialang.org/t/why-is-converting-from-a-type-to-a-symbol-so-slow/103212 "2023-08-25T22:32:15Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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 26, 2023, 12:40am UTC](https://discourse.julialang.org/t/why-is-converting-from-a-type-to-a-symbol-so-slow/103212/2 "2023-08-26T00:40:49Z")

</div>

> [@Tortar](#):
>
> I would have expected some nanoseconds, but instead it takes more than a microsecond: is there a way to make the conversion faster?

`Symbol(x)` for generic `x` falls back to printing `x` to a string, then converting the string to a symbol. This is going to be slow (allocating a new string on each call, among other things).

> [@Tortar](#):
>
> types can’t be compared with each others

If you just want an arbitrary ordering, you can compare by the [`objectid`](https://docs.julialang.org/en/v1/base/base/#Base.objectid) of the type. For example:

```julia
julia> sort(Any[3, 4.2, 6, 5.6, "x", 7, "y"], by=objectid∘typeof)
7-element Vector{Any}:
 3
 6
 7
 4.2
 5.6
  "x"
  "y"

```

Of course, if you have a mixed-type container, such as an array holding elements of different types, you are inherently looking at sub-par Julia performance to start with.

---

_[View the full topic](https://discourse.julialang.org/t/why-is-converting-from-a-type-to-a-symbol-so-slow/103212)._
