-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Closed
Description
Since we already have allunique
, it might make sense to also add allequal
. One reason to add allequal
is that a proper implementation is much faster than the naive length(unique(itr)) == 1
.
function allequal(x)
first, rest = Iterators.peel(x)
for v in rest
if v == first
continue
else
return false
end
end
true
end
slow_allequal(x) = length(unique(x)) == 1
julia> using BenchmarkTools
julia> x = randn(1000); y = fill(1, 1000);
julia> @btime allequal($x);
3.288 ns (0 allocations: 0 bytes)
julia> @btime slow_allequal($x);
36.791 μs (27 allocations: 65.95 KiB)
julia> @btime allequal($y);
475.082 ns (0 allocations: 0 bytes)
julia> @btime slow_allequal($y);
3.816 μs (6 allocations: 608 bytes)
rfourquet and MarcMushrfourquet
Metadata
Metadata
Assignees
Labels
No labels