I am testing the performance difference between priority queue implementation of std::priority_queue vs implementation using std::set.
I have overloaded the < operator to sort based on score, in case when scores are equal use the next argument i. Also I am just calling the push,pop and top function to find the total time taken for these three operations.
In both implementation I am calling this 'timeQueue' function over 200 iterations and averaging the time obtained. I am seeing the std::set implementation takes around 38% faster. What could be the reason. Thank you.
(These are just for testing purpose)
std::set implementation
void timeQueue()
{
NodeData res;
for(int i=1;i<=500000;i++)
{
auto score = rand() %10000000;
NodeData n1 = {i,i,score,i,i};
// push:
priorityList.insert(n1);
}
for(int i=1;i<=500000;i++)
{
std::set<NodeData>::iterator it = priorityList.begin();
// top:
auto s = it->id;
// pop:
priorityList.erase(it);
}
}
Similarly, std::priority_queue
void timeQueue()
{
NodeData res;
for(int i=1;i<=500000;i++)
{
auto score = rand() % 10000;
NodeData n1 = {i,1,score,1,1};
priorityList.push(n1);
}
for(int i=1;i<=500000;i++)
{
res = priorityList.top();
priorityList.pop();
//std::cout<<res.id<<std::endl;
}
}
};
O(n)time for std::priority_queue while it isO(n log n)for std::set. - Jérôme Richard