… depends on the number of events to be handled.
I could not use the PQs I’ve seen in Julia because they did not handle multiple identical event times, so took this as an exercise.
My events have the following structure:
(time, variable_number_and_types_of_args)
I see two performance limiting factors:
- searching in the times for the insertion point
- storing the events with variable number of arguments
The following works, but can we do better?
mutable struct Pqueue
times::Vector{Float16} # event times
data::Vector{Tuple} # event data
end
Pqueue() = Pqueue(Float16[], Any[]) # constructor (empty queue)
function push_pq(pq::Pqueue, t, d)
left = searchsortedfirst(pq.times, t)
insert!(pq.times, left, t)
insert!(pq.data, left, d)
end
function pop_pq(pq::Pqueue)
t = popfirst!(pq.times)
d = popfirst!(pq.data)
return (t, d)
end
function testm(m, n)
pq = Pqueue();
for i = 1:n
if randn() > 0
push_pq(pq, i + m*rand(), Tuple(rand()));
else
push_pq(pq, i + m*rand(), (rand(), 2));
end
if i > m
d = pop_pq(pq);
end
end
println(length(pq.times))
end
# run tests varying number of pending events
for m = Int.([5, 5e1, 5e2, 5e3, 5e4])
@time testm(m, Int(1e6))
end
For example, how to use a single array of struct?
Bad memory, there was already the hint
Will try the timings.
Can we get allocations down?