🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Hundreds of timers...

Started by
28 comments, last by hplus0603 7 years, 9 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?
Advertisement
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