Advertisement

Hundreds of timers...

Started by June 23, 2016 10:06 PM
28 comments, last by hplus0603 8 years, 2 months ago

std::priority_queue<> is pretty easy to use; std::map<> is trivial to use. Both will perform very well.


I comprehend your priority_queue suggestion (thanks for the usage example, btw - I've never used priority queues before), but how/why would one use std::map (or related) in this situation?

Would you map the tick they fire on?
std::unordered_multimap<Tick, Action> timers;

timers.insert(std::make_pair<Tick, Action>(timeToTrigger, actionToTrigger));

//...

auto rangePair = timers.equal_range(currentTick);
for(auto it = rangePair.first; it != rangePair.second; ++it)
{
   it->second->timer_expired();
}
Or what?
Yes -- std::map<> is sorted (it's a red-black tree, typically, as opposed to the heap used for priority_queue.) Thus, the next event to fire will be the begin/leftmost/lower_bound element of the map.
This also means that insertion/lookup in std::map<> is actually O(log N) not O(1) -- it's not a hash table! (You want std::unordered_map<> for that.)

The reason to use std::priority_queue<> over std::map<> is that the "remove the first item and keep the queue ordered" and the "add one item in the right spot in the queuue" operations are some small constant factor faster for heaps than red-black trees. Meanwhile, the general "keep a map ordered and allow random access" is faster for the red-black tree.
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement