Posted on March 17th, 2008 at 23:39 by fr3@K
Source code used to produce results in my previous post is available for download here.
Please note that it requires Boost.Date_Time in order to build and run.
Source code used to produce results in my previous post is available for download here.
Please note that it requires Boost.Date_Time in order to build and run.
這幾天有空, 終於把一項幾個月前就開始做的 benchmark 完成了.
vector 來管理一串 element, 卻硬要把被管理的 element 的 pointer 取出, 以代替 vector 本身的 iterator 來做迭代. 有些同事說是不熟悉 iterator 的特性與操作方式. 這個理由很難讓人接受. 身為職業 C++ programmer, 標準的 container 與 iterator 是無論如何也要掌握住的技巧. 促成我搞這個 benchmark 的原因是有另外的同事跟我說, 取 pointer 出來迭代是因為 vector 的 iterator 比 pointer 慢很多, 並且是指數級別的差距!
在進行這項 benchmark 之前, 說以 vector 的 iterator 迭代比 pointer 慢, (起碼不會快過 pointer) 我絕對不會意外. 我猜大多數人也預期會看這樣的結果. 即便如此, 我還是很難想像其差異會大到如那位同事告訴我的這麼多. 因此我寫了個程式來 benchmark. 將 pointer 以及各種 STL 的 sequence container 的 iterator 做同樣的迭代, 把所使用的時間紀錄下來, 製作成表格以比較其間差異. 讓數字來說話.
(more…)
Except where otherwise noted, COdE fr3@K by
fr3@K is licensed under a
Creative Commons Attribution-Share Alike 3.0 License.