# Set performance question

**URL:** <https://discourse.julialang.org/t/set-performance-question/38989>\
**Category:** New to Julia\
**Created:** [May 7, 2020, 9:19pm UTC](https://discourse.julialang.org/t/set-performance-question/38989 "2020-05-07T21:19:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Charles\_Parker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/charles_parker/32/8350_2.png) [@Charles\_Parker](https://discourse.julialang.org/u/Charles_Parker)\
**Post date:** [May 7, 2020, 9:19pm UTC](https://discourse.julialang.org/t/set-performance-question/38989/1 "2020-05-07T21:19:59Z")

</div>

The Packt book Julia 1.0 Programming Complete Reference Guide has an example on page 95 which I assumed was a misprint.  
x = Set(collect(1:100))  
@time 2 in x

> 0.003186 seconds …  
> x2 = Set(collect(1:1000000))  
> @time 2 in x2  
> 0.000003 seconds …  
> I tried variations and the time for the larger set is less than the smaller set. It’s not a misprint. I assume they’re trying to say set size doesn’t make a difference, but WHY does the larger set keep outperforming the smaller set?

Thanx … Charlie

---

<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:** [May 7, 2020, 9:31pm UTC](https://discourse.julialang.org/t/set-performance-question/38989/2 "2020-05-07T21:31:01Z")

</div>

> [@Charles\_Parker](#):
>
> x = Set(collect(1:100))

Use `BenchmarkTools.jl` for microbenchmarks:

```julia
julia> using BenchmarkTools

julia> s1 = Set(collect(1:100));

julia> s2 = Set(collect(1:100000));

julia> @btime 2 ∈ $s1
  4.082 ns (0 allocations: 0 bytes)
true

julia> @btime 2 ∈ $s2
  4.183 ns (0 allocations: 0 bytes)
true

```

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [May 7, 2020, 9:31pm UTC](https://discourse.julialang.org/t/set-performance-question/38989/3 "2020-05-07T21:31:05Z")

</div>

Checking set inclusion is O(1) so the the size of the set doesn’t really matter. Compilation time is included the first time you call the `in` function to check set inclusion. You generally want to use BenchmarkTools to measure timings.

---

<div class="post-metadata">

**Author:** ![Charles\_Parker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/charles_parker/32/8350_2.png) [@Charles\_Parker](https://discourse.julialang.org/u/Charles_Parker)\
**Post date:** [May 8, 2020, 1:37am UTC](https://discourse.julialang.org/t/set-performance-question/38989/4 "2020-05-08T01:37:44Z")

</div>

Thanx guys. I missed the point about compile time. Feels like the author set me up:)  
… Charlie
