# What is the fastest data type out of these?

**URL:** <https://discourse.julialang.org/t/what-is-the-fastest-data-type-out-of-these/15727>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 1, 2018, 11:03am UTC](https://discourse.julialang.org/t/what-is-the-fastest-data-type-out-of-these/15727 "2018-10-01T11:03:10Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheOnlyArtz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theonlyartz/32/12685_2.png) [@TheOnlyArtz](https://discourse.julialang.org/u/TheOnlyArtz)\
**Post date:** [October 1, 2018, 11:03am UTC](https://discourse.julialang.org/t/what-is-the-fastest-data-type-out-of-these/15727/1 "2018-10-01T11:03:11Z")

</div>

What’s the faster data types out of:

- Dict
- Set

Is there any data type there that is faster than both?  
I would really like to know, thank you.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [October 1, 2018, 11:13am UTC](https://discourse.julialang.org/t/what-is-the-fastest-data-type-out-of-these/15727/2 "2018-10-01T11:13:34Z")

</div>

As Julia is mostly written in Julia, it’s pretty easy to understand what they do:  
[https://github.com/JuliaLang/julia/blob/master/base/dict.jl](https://github.com/JuliaLang/julia/blob/master/base/dict.jl) and [https://github.com/JuliaLang/julia/blob/master/base/set.jl](https://github.com/JuliaLang/julia/blob/master/base/set.jl).

Concerning the performance, best is to check in a setting mimicking your application using BenchmarkTools.jl.

---

<div class="post-metadata">

**Author:** ![TheOnlyArtz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theonlyartz/32/12685_2.png) [@TheOnlyArtz](https://discourse.julialang.org/u/TheOnlyArtz)\
**Post date:** [October 1, 2018, 11:16am UTC](https://discourse.julialang.org/t/what-is-the-fastest-data-type-out-of-these/15727/3 "2018-10-01T11:16:54Z")

</div>

The dataset is dynamic and I worry about performance when it comes to large scale datasets.  
So I pretty much want to know which is faster query-wise

---

<div class="post-metadata">

**Author:** ![laborg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laborg/32/5474_2.png) [@laborg](https://discourse.julialang.org/u/laborg)\
**Post date:** [October 1, 2018, 11:19am UTC](https://discourse.julialang.org/t/what-is-the-fastest-data-type-out-of-these/15727/4 "2018-10-01T11:19:06Z")

</div>

Internally Sets in Julia are implemented using Dicts (only keys and no values). Both use hashing for indexing.

---

<div class="post-metadata">

**Author:** ![TheOnlyArtz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theonlyartz/32/12685_2.png) [@TheOnlyArtz](https://discourse.julialang.org/u/TheOnlyArtz)\
**Post date:** [October 1, 2018, 11:19am UTC](https://discourse.julialang.org/t/what-is-the-fastest-data-type-out-of-these/15727/5 "2018-10-01T11:19:31Z")

</div>

So in theory Dicts should complete a query faster?

---

<div class="post-metadata">

**Author:** ![laborg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laborg/32/5474_2.png) [@laborg](https://discourse.julialang.org/u/laborg)\
**Post date:** [October 1, 2018, 11:22am UTC](https://discourse.julialang.org/t/what-is-the-fastest-data-type-out-of-these/15727/6 "2018-10-01T11:22:13Z")

</div>

I would assume that they offer the same performance. What do you actually mean by _query_?

---

<div class="post-metadata">

**Author:** ![TheOnlyArtz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theonlyartz/32/12685_2.png) [@TheOnlyArtz](https://discourse.julialang.org/u/TheOnlyArtz)\
**Post date:** [October 1, 2018, 11:22am UTC](https://discourse.julialang.org/t/what-is-the-fastest-data-type-out-of-these/15727/7 "2018-10-01T11:22:43Z")

</div>

For example

```julia
a = Dict("key" => "value")
return a["key"]

```

---

<div class="post-metadata">

**Author:** ![laborg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laborg/32/5474_2.png) [@laborg](https://discourse.julialang.org/u/laborg)\
**Post date:** [October 1, 2018, 11:24am UTC](https://discourse.julialang.org/t/what-is-the-fastest-data-type-out-of-these/15727/8 "2018-10-01T11:24:48Z")

</div>

With `a["key"]` on a `Dict` you’ll get the value for the specified key. With a `Set` you can only store a key. They are not the same data structure.

---

<div class="post-metadata">

**Author:** ![TheOnlyArtz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theonlyartz/32/12685_2.png) [@TheOnlyArtz](https://discourse.julialang.org/u/TheOnlyArtz)\
**Post date:** [October 1, 2018, 11:25am UTC](https://discourse.julialang.org/t/what-is-the-fastest-data-type-out-of-these/15727/9 "2018-10-01T11:25:20Z")

</div>

Oh okay I understand, thank you, I guess Dictionary is the way to go

---

<div class="post-metadata">

**Author:** ![TheOnlyArtz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theonlyartz/32/12685_2.png) [@TheOnlyArtz](https://discourse.julialang.org/u/TheOnlyArtz)\
**Post date:** [October 1, 2018, 11:25am UTC](https://discourse.julialang.org/t/what-is-the-fastest-data-type-out-of-these/15727/10 "2018-10-01T11:25:37Z")

</div>

What about `named tuples` tho?

---

<div class="post-metadata">

**Author:** ![laborg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laborg/32/5474_2.png) [@laborg](https://discourse.julialang.org/u/laborg)\
**Post date:** [October 1, 2018, 11:29am UTC](https://discourse.julialang.org/t/what-is-the-fastest-data-type-out-of-these/15727/11 "2018-10-01T11:29:06Z")

</div>

> [@NamedTuples vs Dict](https://discourse.julialang.org/t/namedtuples-vs-dict/13119):
>
> I wondered about use cases of NamedTuples vs Dicts. Obviously one would choose a NamedTuple if the order of elements is important and a Dict if the keys have to be something else than a symbol, but are there any other situations where one is preferred over the other?

I wouldn’t worry about performance in the beginning. Do what you want to do, and if it’s too slow, people in this forum will certainly help you.

---

<div class="post-metadata">

**Author:** ![TheOnlyArtz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theonlyartz/32/12685_2.png) [@TheOnlyArtz](https://discourse.julialang.org/u/TheOnlyArtz)\
**Post date:** [October 1, 2018, 11:33am UTC](https://discourse.julialang.org/t/what-is-the-fastest-data-type-out-of-these/15727/12 "2018-10-01T11:33:56Z")

</div>

Thank you ,  
I will just have large datasets and I need to choose if to store them and choose what data type to choose  
All I need to do is being able to change values by keys and add values by keys

That’s it, so it looks like ~~both `Dict` and `NamedTuples` are cable of these~~ only Dict, so I was wondering what’s faster, looking the post you’ve linked it looks like NamedTuples is way faster.

EDIT: I take back what I said, NamedTuples are immutable, I will go with Dict

---

<div class="post-metadata">

**Author:** ![LeoK987](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leok987/32/4880_2.png) [@LeoK987](https://discourse.julialang.org/u/LeoK987)\
**Post date:** [October 1, 2018, 11:42am UTC](https://discourse.julialang.org/t/what-is-the-fastest-data-type-out-of-these/15727/13 "2018-10-01T11:42:51Z")

</div>

> [@TheOnlyArtz](#):
>
> NamedTuples are immutable

No it’s not

---

<div class="post-metadata">

**Author:** ![TheOnlyArtz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theonlyartz/32/12685_2.png) [@TheOnlyArtz](https://discourse.julialang.org/u/TheOnlyArtz)\
**Post date:** [October 1, 2018, 11:43am UTC](https://discourse.julialang.org/t/what-is-the-fastest-data-type-out-of-these/15727/14 "2018-10-01T11:43:12Z")

</div>

But you can’t change there values according to the post Lanorg linked.

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [October 1, 2018, 11:44am UTC](https://discourse.julialang.org/t/what-is-the-fastest-data-type-out-of-these/15727/15 "2018-10-01T11:44:50Z")

</div>

Named tuples are a good replacement for `Symbol`-keyed `Dict`s if (1) the named tuple is tiny, and (2) the hashing can happen at compile-time.

In all other cases, they have terrible performance compared to a `Dict`. In other words, use them as syntactic sugar to avoid an enum that maps human-readable names to tuple-indices.

---

<div class="post-metadata">

**Author:** ![TheOnlyArtz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theonlyartz/32/12685_2.png) [@TheOnlyArtz](https://discourse.julialang.org/u/TheOnlyArtz)\
**Post date:** [October 1, 2018, 11:45am UTC](https://discourse.julialang.org/t/what-is-the-fastest-data-type-out-of-these/15727/16 "2018-10-01T11:45:40Z")

</div>

Alright, thank you for the clarification

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 1, 2018, 11:54am UTC](https://discourse.julialang.org/t/what-is-the-fastest-data-type-out-of-these/15727/17 "2018-10-01T11:54:36Z")

</div>

> [@LeoK987](#):
>
> > NamedTuples are immutable
> 
> No it’s not

`Tuple`s and `NamedTuple`s are of course immutable, eg

```julia
julia> isimmutable((a = 1, b =2))
true

```

---

<div class="post-metadata">

**Author:** ![LeoK987](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leok987/32/4880_2.png) [@LeoK987](https://discourse.julialang.org/u/LeoK987)\
**Post date:** [October 1, 2018, 11:58am UTC](https://discourse.julialang.org/t/what-is-the-fastest-data-type-out-of-these/15727/18 "2018-10-01T11:58:57Z")

</div>

Thanks for confirming that. So my uses of them have all been unknowingly in the immutable cases then.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [October 1, 2018, 4:35pm UTC](https://discourse.julialang.org/t/what-is-the-fastest-data-type-out-of-these/15727/19 "2018-10-01T16:35:19Z")

</div>

Just benchmark them in your particular application.
