Boost C++ Libraries: Ticket #3518: got terible performace to use multi_index https://svn.boost.org/trac10/ticket/3518 <p> hi, first of all,multi_index is a good idea. when i test it. i got a very bad performance. </p> <p> on the simplest condition,it takes 1.5 ms to 15ms to insert a element. it is too slower than many db,etc sqlite (0.031ms). </p> <p> the code is listed below: </p> <p> <em> testMultiIndex2.cpp : main project file. </em></p> <p> #include "stdafx.h" </p> <p> #if !defined(NDEBUG) #define BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING #define BOOST_MULTI_INDEX_ENABLE_SAFE_MODE #endif </p> <p> #include &lt;boost/multi_index_container.hpp&gt; #include &lt;boost/multi_index/member.hpp&gt; #include &lt;boost/multi_index/ordered_index.hpp&gt; #include &lt;algorithm&gt; #include &lt;iostream&gt; #include &lt;iterator&gt; #include &lt;string&gt; </p> <p> #include &lt;tchar.h&gt; #include "..\..\..\..\..\src\common_cpp\tchar_ex.h" #include "..\..\..\..\..\src\common_cpp2\TsCommonCpp2\TsCommonCpp2\<a class="missing wiki">RecTime</a>.h" </p> <p> #include "..\..\..\..\..\trunk\src\common_cpp\<a class="missing wiki">StrDigEtc</a>.h" </p> <p> using boost::multi_index_container; using namespace boost::multi_index; </p> <p> using namespace std; using namespace System; using namespace dbset; </p> <p> class employee { public: </p> <blockquote> <p> <em>employee() </em>{} </p> </blockquote> <blockquote> <p> employee(int id_,_tstring name_,int age_):id(id_),name(name_),age(age_){} </p> </blockquote> <p> public: </p> <blockquote> <p> int id; _tstring name; int age; </p> </blockquote> <blockquote> <p> friend _tostream&amp; operator&lt;&lt;(_tostream&amp; os,const employee&amp; e) { </p> <blockquote> <p> os&lt;&lt;e.id&lt;&lt;" "&lt;&lt;e.name&lt;&lt;" "&lt;&lt;e.age&lt;&lt;std::endl; return os; </p> </blockquote> <p> } </p> </blockquote> <p> }; </p> <p> struct id{}; struct name{}; struct age{}; </p> <p> typedef multi_index_container&lt; </p> <blockquote> <p> employee, indexed_by&lt; </p> <blockquote> <p> ordered_unique&lt; </p> <blockquote> <p> tag&lt;id&gt;, BOOST_MULTI_INDEX_MEMBER(employee,int,id)&gt; </p> <blockquote> <p> <em>, </em></p> </blockquote> </blockquote> </blockquote> <p> <em> ordered_non_unique&lt; </em> tag&lt;name&gt;,BOOST_MULTI_INDEX_MEMBER(employee,_tstring,name)&gt;, <em> ordered_non_unique&lt; </em> tag&lt;age&gt;, BOOST_MULTI_INDEX_MEMBER(employee,int,age)&gt; </p> <blockquote class="citation"> <p> employee_set; </p> </blockquote> </blockquote> <p> template&lt;typename Tag,typename <a class="missing wiki">MultiIndexContainer</a>&gt; void print_out_by( </p> <blockquote> <p> const <a class="missing wiki">MultiIndexContainer</a>&amp; s, Tag* =0 /* fixes a MSVC++ 6.0 bug with implicit template function parms */ </p> </blockquote> <p> ) { </p> <blockquote> <p> /* obtain a reference to the index tagged by Tag */ </p> </blockquote> <blockquote> <p> const typename boost::multi_index::index&lt;<a class="missing wiki">MultiIndexContainer</a>,Tag&gt;::type&amp; i= </p> <blockquote> <p> get&lt;Tag&gt;(s); </p> </blockquote> </blockquote> <blockquote> <p> typedef typename MultiIndexContainer::value_type value_type; </p> </blockquote> <blockquote> <p> /* dump the elements of the index to cout */ </p> </blockquote> <blockquote> <p> std::copy(i.begin(),i.end(),std::ostream_iterator&lt;value_type&gt;(std::cout)); </p> </blockquote> <p> } </p> <p> int main(array&lt;System::String ^&gt; <sup>args) { </sup></p> <blockquote> <p> Console::<a class="missing wiki">WriteLine</a>(L"start ..."); </p> </blockquote> <blockquote> <blockquote> <p> CRecTime rectimer; </p> </blockquote> </blockquote> <blockquote> <blockquote> <p> rectimer.setStart(); </p> </blockquote> </blockquote> <blockquote> <p> employee_set es; </p> </blockquote> <blockquote> <p> int max; </p> </blockquote> <blockquote> <p> rectimer.setStart(); </p> </blockquote> <blockquote> <p> max = 10; <em>max = 1000; </em>max = 1000 * 10; </p> </blockquote> <blockquote> <p> _tstring name; </p> </blockquote> <blockquote> <p> for(int i = 0, j=max; i &lt; max; i ++,j--) { </p> <blockquote> <p> name = _T("Albert-"); name += StrDigEtc::itoa(i); es.insert(employee(i,name,j)); </p> </blockquote> <p> } </p> </blockquote> <blockquote> <p> rectimer.setStop(); </p> </blockquote> <blockquote> <p> _tcout &lt;&lt; _T("exec time is : ") &lt;&lt; rectimer.getTimePassed() &lt;&lt; endl; _tcout &lt;&lt; _T("perline consume time is : ") &lt;&lt; double(rectimer.getTimePassedNano()) / (double) max &lt;&lt; endl; </p> </blockquote> <blockquote> <p> Console::<a class="missing wiki">WriteLine</a>(L"end ..."); </p> </blockquote> <blockquote> <blockquote> <p> return 0; </p> </blockquote> </blockquote> <p> } </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/3518 Trac 1.4.3 david.joe <zhouyong_ts@…> Sat, 10 Oct 2009 03:25:27 GMT <link>https://svn.boost.org/trac10/ticket/3518#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3518#comment:1</guid> <description> <p> moreover, it runs on winxp sp3 , visual c++ 2008 ,boost 1.40.0 ,and cpu is amd 3600+,ram 3g </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Joaquín M López Muñoz</dc:creator> <pubDate>Sat, 10 Oct 2009 16:05:07 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/3518#comment:2 https://svn.boost.org/trac10/ticket/3518#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">invalid</span> </li> </ul> <p> I understand you're running your test program in debug mode. If so, invariant-checking mode and safe mode (enabled in your code by defining BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING and BOOST_MULTI_INDEX_ENABLE_SAFE_MODE), and in particular the former, will greatly decrease performance. These are meant to be used as debugging aids, not in production mode. Please turn them off and recheck. Also, performance should be much better in release mode, both because it is release mode after all and because you're not defining the aforementioned macros in that case. </p> <p> I'm closing this ticket, please reopen if you find more to the issue than described above. </p> Ticket