473,407 Members | 2,315 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Unexpected pop_head performance compared to push_heap

Hi people,
I have some piece of code that takes way too much time to do what it
does (which is irrelevant here) and while searching for the cause I
stumbled upon the following surprising results:

The code uses a priority queue and so have lines of the form:

PQ.push(item);

and

Item item = PQ.top() ; PQ.pop();

So I added some manual profiling code around those two operations:

Timer t ; t.start();
PQ.push(item)
t.stop(); total_insert_time += t.time();

Timer t ; t.start();
Item item = PQ.top() ; PQ.pop();
t.stop(); total_remove_time += t.time();

During the particularly long run, about 150000 items are inserted in
total and each and every one of them removed. (the main loop finishes
when the PQ is empty)

But here is the puzzing (to me at least) result:

total_insert_time : 34 seconds
total_remove_time: 240 seconds

And this ratio is more or less consistent, not only among several runs
of the same algorithm with the same input in my mahcine but among
several runs on different platforms with different STL implementations
and machines.

FWIW, in all the different paltforms the code is compiled with the
default options, which in many cases means a range checked STL.

What I found very odd here is that pop_heap takes 8-10 times longer
than push_heap in an escenario where total the number of items are
balanced (the PQ is consumed until it's empty)

And there's more... it turns out that 95% of the items are insterted in
a initialization phase before the first is ever removed..... after the
first remove, a run of items may be inserted for each remove, but only
5% of the total is inserted in this phase.
I tried putting all the items from the initialization phase (95% of the
total) in a vector, with storage reserved in advance for 200000 items
to avoid additional allocations, then constructing the PQ with that
sequence (which calls make_heap up front), but the results are roughly
the same... removing all of the items takes 8-10 times longer than
inserting them.

Is this expected?
What can I do?

Given that 95% of the items are inserted up front, could using a set<>
and implementing the pop_heap() as a linear search for the first
unflagged item be better? (flagging items instead of removing them to
avoid tree rebalancing)... or maybe using a manually sorted list so
that removal becomes O(1) even if inserting is O(n.log n) instead of
O(log n) as in the heap?

TIA

Fernando Cacciola
SciSoft
http://fcacciola.50webs.com

Jun 22 '06 #1
0 1263

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Marc H. | last post by:
Hello, I recently converted one of my perl scripts to python. What the script does is simply search a lot of big mail files (~40MB) to retrieve specific emails. I simply converted the script...
37
by: Kevin C | last post by:
Quick Question: StringBuilder is obviously more efficient dealing with string concatenations than the old '+=' method... however, in dealing with relatively large string concatenations (ie,...
2
by: Kenneth Massey | last post by:
I was noticing significantly worse performance in some of my C++ codes compiled with gcc 3.4.3 as compared to gcc 3.3.4. I have boiled it down into one relatively short code that illustrates. It...
1
by: jd | last post by:
I have an xsl file to generate xml in to an html file. The size of the xsl is 300kb and size of the xml is 47 kb (the size of xml is variable). I am using VB.net to convert the xml file into an...
4
by: smoody | last post by:
I have an ASP.NET application which uses a .NET Web Service as a wrapper to a COM+ DLL. Performance for a single user is very good. However, I have noticed that when multiple requests are made...
14
by: Greg | last post by:
1. What work is in progress to bring C# performance closer to C++/CLI? Can we expect C# to equal C++/CLI performance? If so when? 2. Also what work is in progress to improve MSIL/CLR/JIT...
5
by: toton | last post by:
Hi, I want a few of my class to overload from a base class, where the base class contains common functionality. This is to avoid repetition of code, and may be reducing amount of code in binary,...
20
by: Jim Michaels | last post by:
I have a 638 line glob of PHP code & HTML that won't run. I get "PHP Parse error: syntax error, unexpected '}' in quiz\\quiz.php on line 594". I wrote a brace checker that checks perens, square...
4
by: George2 | last post by:
Hello everyone, I want to know why virtual function's footprint and performance is compared with normal function call. Here is my analysis, please review whether I am correct. Thanks. 1....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.