-
Notifications
You must be signed in to change notification settings - Fork 246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PriorityQueue
s iteration slower than necessary
#792
Comments
It makes a copy of If all you want is the first item, use |
Can this be closed? |
|
It seems rather difficult to iterate over a heap without destroying it, and the result would likely be much less efficient than the destructive algorithm. |
Is there an easy way to get the unordered iteration? The easiest I could come up with is julia> f(flag) = ( a= iterate(c_pq, flag);
while true
a === nothing && break
a = iterate(c_pq, a[2])
end)
f (generic function with 1 method)
julia> @btime f(false)
9.013 μs (2000 allocations: 62.50 KiB)
julia> @btime f(true)
390.045 μs (1205 allocations: 118.47 KiB) |
I'm probably missing something here, but even this is sorting after the fact is faster than the current implementation julia> pq = PriorityQueue(string(Char(i+96)) => rand() for i in 1:10)
julia> @btime sort!([DataStructures.percolate_down!(pq, i) for i in 1:length(pq)], by=x->x.second)
980.364 ns (16 allocations: 720 bytes)
10-element Vector{Pair{Any, Any}}:
"j" => 0.24851360627935248
"f" => 0.25429092151530586
"e" => 0.2889706212235992
"a" => 0.3324694434948622
⋮
"d" => 0.4278330560665746
"i" => 0.5729941482452947
"c" => 0.719639457382443
julia> @btime [x for x in pq]
1.274 μs (9 allocations: 1.05 KiB)
10-element Vector{Pair{Any, Any}}:
"j" => 0.24851360627935248
"f" => 0.25429092151530586
"e" => 0.2889706212235992
"a" => 0.3324694434948622
⋮
"d" => 0.4278330560665746
"i" => 0.5729941482452947
"c" => 0.719639457382443 |
I don't know of one, but I haven't looked.
I'll reopen, in case you've discovered a faster nondestructive sort. |
SortedSet, SortedDict, or SortedMultiDict might be a solution (depending on your intended use). |
Compare these
The text was updated successfully, but these errors were encountered: