Set performance question

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

Use BenchmarkTools.jl for microbenchmarks:

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
2 Likes

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.

3 Likes

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