我特林在配對堆的C++實現,我從這裏開始了: http://home.fnal.gov/~stoughto/build/graphviz-2.22.2/lib/vpsc/pairingheap/PairingHeap.h http://home.fnal.gov/~stoughto/build/graphviz-2.22.2/lib/vpsc/pairingheap/PairingHeap.cpp配對堆VS的std :: priority_queue
我比較這對PairingHeap一個std :: priority_queue 和這些結果如下:
GCC 4.7 -O3,核i7的2.4Ghz rdstc指令以測量週期
-------------------------------------------------------------------------------
for 100.000 elements:
o std::priority_queue<int>
- insert: 9,800,415 cycles
- extract: 29,712,818 cycles
- total: 39,513,233 cycles [0.031secs]
o PairingHeap<int>
- insert: 34,381,467 cycles
- extract: 259,986,113 cycles
- total: 294,367,580 cycles [0.125secs]
-------------------------------------------------------------------------------
for 1.000.000 elements:
o std::priority_queue<int>
- insert: 95,954,533 cycles
- extract: 518,546,747 cycles
- total: 614,501,280 cycles [0.296secs]
o PairingHeap<int>
- insert: 344,453,782 cycles
- extract: 3,856,344,199 cycles
- total: 4,200,797,981 cycles [1.593secs]
-------------------------------------------------------------------------------
for 10.000.000 elements:
o std::priority_queue<int>
- insert: 999,836,450 cycles
- extract: 10,634,407,049 cycles
- total: 11,634,243,499 cycles [4.390secs]
o PairingHeap<int>
- insert: 3,441,903,781 cycles
- extract: 61,166,421,272 cycles
- total: 64,608,325,053 cycles [24.187secs]
配對堆應該比std :: priority_queue快,因爲它應該有漸近更快的運行速度,但相反,這裏的配對堆是,非常慢,因此會更慢。 我認爲這是因爲std :: priority_queue在引擎蓋下使用了一個向量,並且這比緩衝區更友好,比爲每個整數分配節點,就像配對堆一樣。
所以,我的問題是:漸近地更好的數據結構可以被更壞的數據結構(很大程度上)打敗, 只是因爲它們更加緩存友好? 真的值得花費很多時間在一個更復雜的數據結構中,比如配對堆,當 默認的std :: priority_queue可以在很大程度上比它快嗎?
我只是沒有考慮到我使用的配對堆的實現只是廢話, 但它似乎不是,因爲我嘗試過的其他實現更糟! 想法?