How to count the interval number in sites not using for loop and push!?

I have an interval and some sites,I want to judge whether each of the interval are in sites,and finally count the number.like the following

using BenchmarkTools
intervals=10650:10699
sites=[10673,10655,10662,10670,10650,10691,10694,10689,10660,10667,10679]
function m(intervals,sites)
b=Bool[]
for i in intervals
if i in sites
push!(b,true)
else
push!(b,false)
end
end
sum(b)
end
julia> @btime m($intervals,$sites)
566.754 ns (4 allocations: 352 bytes)
11


I got an easier way.But it was slower.

julia> @btime length(intersect($intervals,$sites))
1.979 μs (21 allocations: 3.48 KiB)
11


Are there any easier ways (not using for loop as well as push!)?Thanks for helping me!

Try:

count(∈(intervals), sites)


(∈ is typed \in in REPL)

4 Likes