performance.html 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0.1 Transitional//EN">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  5. <title>Boost.MultiIndex Documentation - Performance</title>
  6. <link rel="stylesheet" href="style.css" type="text/css">
  7. <link rel="start" href="index.html">
  8. <link rel="prev" href="compiler_specifics.html">
  9. <link rel="up" href="index.html">
  10. <link rel="next" href="examples.html">
  11. </head>
  12. <body>
  13. <h1><img src="../../../boost.png" alt="boost.png (6897 bytes)" align=
  14. "middle" width="277" height="86">Boost.MultiIndex Performance</h1>
  15. <div class="prev_link"><a href="compiler_specifics.html"><img src="prev.gif" alt="compiler specifics" border="0"><br>
  16. Compiler specifics
  17. </a></div>
  18. <div class="up_link"><a href="index.html"><img src="up.gif" alt="index" border="0"><br>
  19. Index
  20. </a></div>
  21. <div class="next_link"><a href="examples.html"><img src="next.gif" alt="examples" border="0"><br>
  22. Examples
  23. </a></div><br clear="all" style="clear: all;">
  24. <hr>
  25. <h2>Contents</h2>
  26. <ul>
  27. <li><a href="#intro">Introduction</a></li>
  28. <li><a href="#simulation">Manual simulation of a <code>multi_index_container</code></a></li>
  29. <li><a href="#spatial_efficiency">Spatial efficiency</a></li>
  30. <li><a href="#time_efficiency">Time efficiency</a></li>
  31. <li><a href="#tests">Performance tests</a>
  32. <ul>
  33. <li><a href="#test_1r">Results for 1 ordered index</a>
  34. <ul>
  35. <li><a href="#memory_1r">Memory consumption</a></li>
  36. <li><a href="#time_1r">Execution time</a></li>
  37. </ul>
  38. </li>
  39. <li><a href="#test_1s">Results for 1 sequenced index</a>
  40. <ul>
  41. <li><a href="#memory_1s">Memory consumption</a></li>
  42. <li><a href="#time_1s">Execution time</a></li>
  43. </ul>
  44. </li>
  45. <li><a href="#test_2r">Results for 2 ordered indices</a>
  46. <ul>
  47. <li><a href="#memory_2r">Memory consumption</a></li>
  48. <li><a href="#time_2r">Execution time</a></li>
  49. </ul>
  50. </li>
  51. <li><a href="#test_1r1s">Results for 1 ordered index + 1 sequenced index</a>
  52. <ul>
  53. <li><a href="#memory_1r1s">Memory consumption</a></li>
  54. <li><a href="#time_1r1s">Execution time</a></li>
  55. </ul>
  56. </li>
  57. <li><a href="#test_3r">Results for 3 ordered indices</a>
  58. <ul>
  59. <li><a href="#memory_3r">Memory consumption</a></li>
  60. <li><a href="#time_3r">Execution time</a></li>
  61. </ul>
  62. </li>
  63. <li><a href="#test_2r1s">Results for 2 ordered indices + 1 sequenced index</a>
  64. <ul>
  65. <li><a href="#memory_2r1s">Memory consumption</a></li>
  66. <li><a href="#time_2r1s">Execution time</a></li>
  67. </ul>
  68. </li>
  69. </ul>
  70. </li>
  71. <li><a href="#conclusions">Conclusions</a></li>
  72. </ul>
  73. <h2><a name="intro">Introduction</a></h2>
  74. <p>
  75. Boost.MultiIndex helps the programmer to avoid the manual construction of cumbersome
  76. compositions of containers when multi-indexing capabilities are needed. Furthermore,
  77. it does so in an efficient manner, both in terms of space and time consumption. The
  78. space savings stem from the compact representation of the underlying data structures,
  79. requiring a single node per element. As for time efficiency, Boost.MultiIndex
  80. intensively uses metaprogramming techniques producing very tight implementations
  81. of member functions which take care of the elementary operations for each index:
  82. for <code>multi_index_container</code>s with two or more indices, the running time
  83. can be reduced to half as long as with manual simulations involving several
  84. STL containers.
  85. </p>
  86. <h2><a name="simulation">Manual simulation of a <code>multi_index_container</code></a></h2>
  87. <p>
  88. The section on <a href="tutorial/techniques.html#emulate_std_containers">emulation
  89. of standard containers with <code>multi_index_container</code></a> shows the equivalence
  90. between single-index <code>multi_index_container</code>s and some STL containers. Let us now
  91. concentrate on the problem of simulating a <code>multi_index_container</code> with two
  92. or more indices with a suitable combination of standard containers.
  93. </p>
  94. <p>
  95. Consider the following instantiation of <code>multi_index_container</code>:
  96. </p>
  97. <blockquote><pre>
  98. <span class=keyword>typedef</span> <span class=identifier>multi_index_container</span><span class=special>&lt;</span>
  99. <span class=keyword>int</span><span class=special>,</span>
  100. <span class=identifier>indexed_by</span><span class=special>&lt;</span>
  101. <span class=identifier>ordered_unique</span><span class=special>&lt;</span><span class=identifier>identity</span><span class=special>&lt;</span><span class=keyword>int</span><span class=special>&gt;</span> <span class=special>&gt;,</span>
  102. <span class=identifier>ordered_non_unique</span><span class=special>&lt;</span><span class=identifier>identity</span><span class=special>&lt;</span><span class=keyword>int</span><span class=special>&gt;,</span> <span class=identifier>std</span><span class=special>::</span><span class=identifier>greater</span> <span class=special>&gt;,</span>
  103. <span class=special>&gt;</span>
  104. <span class=special>&gt;</span> <span class=identifier>indexed_t</span><span class=special>;</span>
  105. </pre></blockquote>
  106. <p>
  107. <code>indexed_t</code> maintains two internal indices on elements of type
  108. <code>int</code>. In order to simulate this data structure resorting only to
  109. standard STL containers, one can use on a first approach the following types:
  110. </p>
  111. <blockquote><pre>
  112. <span class=comment>// dereferencing compare predicate</span>
  113. <span class=keyword>template</span><span class=special>&lt;</span><span class=keyword>typename</span> <span class=identifier>Iterator</span><span class=special>,</span><span class=keyword>typename</span> <span class=identifier>Compare</span><span class=special>&gt;</span>
  114. <span class=keyword>struct</span> <span class=identifier>it_compare</span>
  115. <span class=special>{</span>
  116. <span class=keyword>bool</span> <span class=keyword>operator</span><span class=special>()(</span><span class=keyword>const</span> <span class=identifier>Iterator</span><span class=special>&amp;</span> <span class=identifier>x</span><span class=special>,</span><span class=keyword>const</span> <span class=identifier>Iterator</span><span class=special>&amp;</span> <span class=identifier>y</span><span class=special>)</span><span class=keyword>const</span>
  117. <span class=special>{</span>
  118. <span class=keyword>return</span> <span class=identifier>comp</span><span class=special>(*</span><span class=identifier>x</span><span class=special>,*</span><span class=identifier>y</span><span class=special>);</span>
  119. <span class=special>}</span>
  120. <span class=keyword>private</span><span class=special>:</span>
  121. <span class=identifier>Compare</span> <span class=identifier>comp</span><span class=special>;</span>
  122. <span class=special>};</span>
  123. <span class=keyword>typedef</span> <span class=identifier>std</span><span class=special>::</span><span class=identifier>set</span><span class=special>&lt;</span><span class=keyword>int</span><span class=special>&gt;</span> <span class=identifier>manual_t1</span><span class=special>;</span> <span class=comment>// equivalent to indexed_t's index #0</span>
  124. <span class=keyword>typedef</span> <span class=identifier>std</span><span class=special>::</span><span class=identifier>multiset</span><span class=special>&lt;</span>
  125. <span class=keyword>const</span> <span class=keyword>int</span><span class=special>*,</span>
  126. <span class=identifier>it_compare</span><span class=special>&lt;</span>
  127. <span class=keyword>const</span> <span class=keyword>int</span><span class=special>*,</span>
  128. <span class=identifier>std</span><span class=special>::</span><span class=identifier>greater</span><span class=special>&lt;</span><span class=keyword>int</span><span class=special>&gt;</span>
  129. <span class=special>&gt;</span>
  130. <span class=special>&gt;</span> <span class=identifier>manual_t2</span><span class=special>;</span> <span class=comment>// equivalent to indexed_t's index #1</span>
  131. </pre></blockquote>
  132. <p>
  133. where <code>manual_t1</code> is the "base" container that holds
  134. the actual elements, and <code>manual_t2</code> stores pointers to
  135. elements of <code>manual_t1</code>. This scheme turns out to be quite
  136. inefficient, though: while insertion into the data structure is simple enough:
  137. </p>
  138. <blockquote><pre>
  139. <span class=identifier>manual_t1</span> <span class=identifier>c1</span><span class=special>;</span>
  140. <span class=identifier>manual_t2</span> <span class=identifier>c2</span><span class=special>;</span>
  141. <span class=comment>// insert the element 5</span>
  142. <span class=identifier>manual_t1</span><span class=special>::</span><span class=identifier>iterator</span> <span class=identifier>it1</span><span class=special>=</span><span class=identifier>c1</span><span class=special>.</span><span class=identifier>insert</span><span class=special>(</span><span class=number>5</span><span class=special>).</span><span class=identifier>first</span><span class=special>;</span>
  143. <span class=identifier>c2</span><span class=special>.</span><span class=identifier>insert</span><span class=special>(&amp;*</span><span class=identifier>it1</span><span class=special>);</span>
  144. </pre></blockquote>
  145. deletion, on the other hand, necessitates a logarithmic search, whereas
  146. <code>indexed_t</code> deletes in constant time:
  147. <blockquote><pre>
  148. <span class=comment>// remove the element pointed to by it2</span>
  149. <span class=identifier>manual_t2</span><span class=special>::</span><span class=identifier>iterator</span> <span class=identifier>it2</span><span class=special>=...;</span>
  150. <span class=identifier>c1</span><span class=special>.</span><span class=identifier>erase</span><span class=special>(**</span><span class=identifier>it2</span><span class=special>);</span> <span class=comment>// watch out! performs in logarithmic time</span>
  151. <span class=identifier>c2</span><span class=special>.</span><span class=identifier>erase</span><span class=special>(</span><span class=identifier>it2</span><span class=special>);</span>
  152. </pre></blockquote>
  153. <p>
  154. The right approach consists of feeding the second container not with
  155. raw pointers, but with elements of type <code>manual_t1::iterator</code>:
  156. </p>
  157. <blockquote><pre>
  158. <span class=keyword>typedef</span> <span class=identifier>std</span><span class=special>::</span><span class=identifier>set</span><span class=special>&lt;</span><span class=keyword>int</span><span class=special>&gt;</span> <span class=identifier>manual_t1</span><span class=special>;</span> <span class=comment>// equivalent to indexed_t's index #0</span>
  159. <span class=keyword>typedef</span> <span class=identifier>std</span><span class=special>::</span><span class=identifier>multiset</span><span class=special>&lt;</span>
  160. <span class=identifier>manual_t1</span><span class=special>::</span><span class=identifier>iterator</span><span class=special>,</span>
  161. <span class=identifier>it_compare</span><span class=special>&lt;</span>
  162. <span class=identifier>manual_t1</span><span class=special>::</span><span class=identifier>iterator</span><span class=special>,</span>
  163. <span class=identifier>std</span><span class=special>::</span><span class=identifier>greater</span><span class=special>&lt;</span><span class=keyword>int</span><span class=special>&gt;</span>
  164. <span class=special>&gt;</span>
  165. <span class=special>&gt;</span> <span class=identifier>manual_t2</span><span class=special>;</span> <span class=comment>// equivalent to indexed_t's index #1</span>
  166. </pre></blockquote>
  167. <p>
  168. Now, insertion and deletion can be performed with complexity bounds
  169. equivalent to those of <code>indexed_t</code>:
  170. </p>
  171. <blockquote><pre>
  172. <span class=identifier>manual_t1</span> <span class=identifier>c1</span><span class=special>;</span>
  173. <span class=identifier>manual_t2</span> <span class=identifier>c2</span><span class=special>;</span>
  174. <span class=comment>// insert the element 5</span>
  175. <span class=identifier>manual_t1</span><span class=special>::</span><span class=identifier>iterator</span> <span class=identifier>it1</span><span class=special>=</span><span class=identifier>c1</span><span class=special>.</span><span class=identifier>insert</span><span class=special>(</span><span class=number>5</span><span class=special>).</span><span class=identifier>first</span><span class=special>;</span>
  176. <span class=identifier>c2</span><span class=special>.</span><span class=identifier>insert</span><span class=special>(</span><span class=identifier>it1</span><span class=special>);</span>
  177. <span class=comment>// remove the element pointed to by it2</span>
  178. <span class=identifier>manual_t2</span><span class=special>::</span><span class=identifier>iterator</span> <span class=identifier>it2</span><span class=special>=...;</span>
  179. <span class=identifier>c1</span><span class=special>.</span><span class=identifier>erase</span><span class=special>(*</span><span class=identifier>it2</span><span class=special>);</span> <span class=comment>// OK: constant time</span>
  180. <span class=identifier>c2</span><span class=special>.</span><span class=identifier>erase</span><span class=special>(</span><span class=identifier>it2</span><span class=special>);</span>
  181. </pre></blockquote>
  182. <p>
  183. The construction can be extended in a straightforward manner to
  184. handle more than two indices. In what follows, we will compare
  185. instantiations of <code>multi_index_container</code> against this sort of
  186. manual simulations.
  187. </p>
  188. <h2><a name="spatial_efficiency">Spatial efficiency</a></h2>
  189. <p>
  190. The gain in space consumption of <code>multi_index_container</code> with
  191. respect to its manual simulations is amenable to a very simple
  192. theoretical analysis. For simplicity, we will ignore alignment
  193. issues (which in general play in favor of <code>multi_index_container</code>.)
  194. </p>
  195. <p>
  196. Nodes of a <code>multi_index_container</code> with <i>N</i> indices hold the value
  197. of the element plus <i>N</i> headers containing linking information for
  198. each index. Thus the node size is
  199. </p>
  200. <blockquote>
  201. <i>S<sub>I</sub></i> = <i>e</i> + <i>h</i><sub>0</sub> + ··· +
  202. <i>h</i><sub><i>N</i>-1</sub>, where<br>
  203. <i>e</i> = size of the element,<br>
  204. <i>h</i><sub><i>i</i></sub> = size of the <i>i</i>-th header.
  205. </blockquote>
  206. <p>
  207. On the other hand, the manual simulation allocates <i>N</i> nodes per
  208. element, the first holding the elements themselves and the rest
  209. storing iterators to the "base" container. In practice, an iterator
  210. merely holds a raw pointer to the node it is associated to, so its size
  211. is independent of the type of the elements. Summing all contributions,
  212. the space allocated per element in a manual simulation is
  213. </p>
  214. <blockquote>
  215. <i>S<sub>M</sub></i> = (<i>e</i> + <i>h</i><sub>0</sub>) +
  216. (<i>p</i> + <i>h</i><sub>1</sub>) + ··· +
  217. (<i>p</i> + <i>h</i><sub><i>N</i>-1</sub>) =
  218. <i>S<sub>I</sub></i> + (<i>N</i>-1)<i>p</i>, where<br>
  219. <i>p</i> = size of a pointer.<br>
  220. </blockquote>
  221. <p>
  222. The relative amount of memory taken up by <code>multi_index_container</code>
  223. with respect to its manual simulation is just
  224. <i>S<sub>I</sub></i>&nbsp;/&nbsp;<i>S<sub>M</sub></i>, which can be expressed
  225. then as:
  226. </p>
  227. <blockquote>
  228. <i>S<sub>I</sub></i>&nbsp;/&nbsp;<i>S<sub>M</sub></i> =
  229. <i>S<sub>I</sub></i>&nbsp;/&nbsp;(<i>S<sub>I</sub></i> + (<i>N</i>-1)<i>p</i>).
  230. </blockquote>
  231. <p>
  232. The formula shows that <code>multi_index_container</code> is more efficient
  233. with regard to memory consumption as the number of indices grow. An implicit
  234. assumption has been made that headers of <code>multi_index_container</code>
  235. index nodes are the same size that their analogues in STL containers; but there
  236. is a particular case in which this is often not the case: ordered indices use a
  237. <a href="tutorial/indices.html#ordered_node_compression">spatial optimization
  238. technique</a> which is not present in many implementations of
  239. <code>std::set</code>, giving an additional advantage to
  240. <code>multi_index_container</code>s of one system word per ordered index.
  241. Taking this fact into account, the former formula can be adjusted to:
  242. </p>
  243. <blockquote>
  244. <i>S<sub>I</sub></i>&nbsp;/&nbsp;<i>S<sub>M</sub></i> =
  245. <i>S<sub>I</sub></i>&nbsp;/&nbsp;(<i>S<sub>I</sub></i> + (<i>N</i>-1)<i>p</i> + <i>Ow</i>),
  246. </blockquote>
  247. <p>
  248. where <i>O</i> is the number of ordered indices of the container, and <i>w</i>
  249. is the system word size (typically 4 bytes on 32-bit architectures.)
  250. </p>
  251. <p>
  252. These considerations have overlooked an aspect of the greatest practical
  253. importance: the fact that <code>multi_index_container</code> allocates a single
  254. node per element, compared to the many nodes of different sizes
  255. built by manual simulations, diminishes memory fragmentation, which
  256. can show up in more usable memory available and better performance.
  257. </p>
  258. <h2><a name="time_efficiency">Time efficiency</a></h2>
  259. <p>
  260. From the point of view of computational complexity (i.e. big-O
  261. characterization), <code>multi_index_container</code> and its corresponding manual
  262. simulations are equivalent: inserting an element into
  263. a <code>multi_index_container</code> reduces to a simple combination of
  264. elementary insertion operations on each of the indices, and
  265. similarly for deletion. Hence, the most we can expect is a reduction
  266. (or increase) of execution time by a roughly constant factor. As we
  267. will see later, the reduction can be very significative for
  268. <code>multi_index_container</code>s with two or more indices.
  269. </p>
  270. <p>In the special case of <code>multi_index_container</code>s with only one index,
  271. resulting performance will roughly match that of the STL equivalent containers:
  272. tests show that there is at most a negligible degradation with respect to STL,
  273. and even in some cases a small improvement.
  274. </p>
  275. <h2><a name="tests">Performance tests</a></h2>
  276. <p>
  277. See <a href="../perf/test_perf.cpp">source code</a> used for measurements.
  278. <p>
  279. In order to assess the efficiency of <code>multi_index_container</code>, the following
  280. basic algorithm
  281. </p>
  282. <blockquote><pre>
  283. <span class=identifier>multi_index_container</span><span class=special>&lt;...&gt;</span> <span class=identifier>c</span><span class=special>;</span>
  284. <span class=keyword>for</span><span class=special>(</span><span class=keyword>int</span> <span class=identifier>i</span><span class=special>=</span><span class=number>0</span><span class=special>;</span><span class=identifier>i</span><span class=special>&lt;</span><span class=identifier>n</span><span class=special>;++</span><span class=identifier>i</span><span class=special>)</span><span class=identifier>c</span><span class=special>.</span><span class=identifier>insert</span><span class=special>(</span><span class=identifier>i</span><span class=special>);</span>
  285. <span class=keyword>for</span><span class=special>(</span><span class=identifier>iterator</span> <span class=identifier>it</span><span class=special>=</span><span class=identifier>c</span><span class=special>.</span><span class=identifier>begin</span><span class=special>();</span><span class=identifier>it</span><span class=special>!=</span><span class=identifier>c</span><span class=special>.</span><span class=identifier>end</span><span class=special>();)</span><span class=identifier>c</span><span class=special>.</span><span class=identifier>erase</span><span class=special>(</span><span class=identifier>it</span><span class=special>++);</span>
  286. </pre></blockquote>
  287. <p>
  288. has been measured for different instantiations of <code>multi_index_container</code>
  289. at values of <i>n</i> 1,000, 10,000 and 100,000,
  290. and its execution time compared with that of the equivalent algorithm
  291. for the corresponding manual simulation of the data structure based on
  292. STL containers. The table below describes the test environments used.
  293. </p>
  294. <p align="center">
  295. <table cellspacing="0" cellpadding="5">
  296. <caption><b>Tests environments.</b></caption>
  297. <tr>
  298. <th>Compiler</th>
  299. <th>Settings</th>
  300. <th>OS and CPU</th>
  301. </tr>
  302. <tr>
  303. <td>GCC 3.4.5 (mingw special)</td>
  304. <td><code>-O3</code></td>
  305. <td>Windows 2000 Pro on P4 1.5 GHz, 256 MB RAM</td>
  306. </tr>
  307. <tr class="odd_tr">
  308. <td>Intel C++ 7.1</td>
  309. <td>default release settings</td>
  310. <td>Windows 2000 Pro on P4 1.5 GHz, 256 MB RAM</td>
  311. </tr>
  312. <tr>
  313. <td>Microsoft Visual C++ 8.0</td>
  314. <td>default release settings, <code>_SECURE_SCL=0</code></td>
  315. <td>Windows XP on P4 Xeon 3.2 GHz, 1 GB RAM</td>
  316. </tr>
  317. </table>
  318. </p>
  319. <p>
  320. The relative memory consumption (i.e. the amount of memory allocated
  321. by a <code>multi_index_container</code> with respect to its manual simulation)
  322. is determined by dividing the size of a <code>multi_index_container</code> node
  323. by the sum of node sizes of all the containers integrating the
  324. simulating data structure.
  325. </p>
  326. <h3><a name="test_1r">Results for 1 ordered index</a></h3>
  327. <p>
  328. The following instantiation of <code>multi_index_container</code> was tested:
  329. </p>
  330. <blockquote><pre>
  331. <span class=identifier>multi_index_container</span><span class=special>&lt;</span>
  332. <span class=keyword>int</span><span class=special>,</span>
  333. <span class=identifier>indexed_by</span><span class=special>&lt;</span>
  334. <span class=identifier>ordered_unique</span><span class=special>&lt;</span><span class=identifier>identity</span><span class=special>&lt;</span><span class=keyword>int</span><span class=special>&gt;</span> <span class=special>&gt;</span>
  335. <span class=special>&gt;</span>
  336. <span class=special>&gt;</span>
  337. </pre></blockquote>
  338. <p>
  339. which is functionally equivalent to <code>std::set&lt;int></code>.
  340. </p>
  341. <h4><a name="memory_1r">Memory consumption</a></h4>
  342. <p align="center">
  343. <table cellspacing="0">
  344. <tr>
  345. <th width="33%">GCC 3.4.5</th>
  346. <th width="33%">ICC 7.1</th>
  347. <th width="33%">MSVC 8.0</th>
  348. </tr>
  349. <tr>
  350. <td align="center">80%</td>
  351. <td align="center">80%</td>
  352. <td align="center">80%</td>
  353. </tr>
  354. </table>
  355. <b>Table 1: Relative memory consumption of <code>multi_index_container</code> with 1
  356. ordered index.</b>
  357. </p>
  358. <p>
  359. The reduction in memory usage is accounted for by the optimization technique implemented
  360. in Boost.MultiIndex ordered indices, as <a href="#spatial_efficiency">explained above</a>.
  361. </p>
  362. <h4><a name="time_1r">Execution time</a></h4>
  363. <p align="center">
  364. <img src="perf_1o.png" alt="performance of multi_index_container with 1 ordered index"
  365. width="556" height="372"><br>
  366. <b>Fig. 1: Performance of <code>multi_index_container</code> with 1 ordered index.</b>
  367. </p>
  368. <p>
  369. Somewhat surprisingly, <code>multi_index_container</code> performs slightly
  370. better than <code>std::set</code>. A very likely explanation for this behavior
  371. is that the lower memory consumption of <code>multi_index_container</code>
  372. results in a higher processor cache hit rate.
  373. The improvement is smallest for GCC, presumably because the worse quality of
  374. this compiler's optimizer masks the cache-related benefits.
  375. </p>
  376. <h3><a name="test_1s">Results for 1 sequenced index</a></h3>
  377. <p>
  378. The following instantiation of <code>multi_index_container</code> was tested:
  379. </p>
  380. <blockquote><pre>
  381. <span class=identifier>multi_index_container</span><span class=special>&lt;</span>
  382. <span class=keyword>int</span><span class=special>,</span>
  383. <span class=identifier>indexed_by</span><span class=special>&lt;</span>
  384. <span class=identifier>sequenced</span><span class=special>&lt;&gt;</span>
  385. <span class=special>&gt;</span>
  386. <span class=special>&gt;</span>
  387. </pre></blockquote>
  388. <p>
  389. which is functionally equivalent to <code>std::list&lt;int></code>.
  390. </p>
  391. <h4><a name="memory_1s">Memory consumption</a></h4>
  392. <p align="center">
  393. <table cellspacing="0">
  394. <tr>
  395. <th width="33%">GCC 3.4.5</th>
  396. <th width="33%">ICC 7.1</th>
  397. <th width="33%">MSVC 8.0</th>
  398. </tr>
  399. <tr>
  400. <td align="center">100%</td>
  401. <td align="center">100%</td>
  402. <td align="center">100%</td>
  403. </tr>
  404. </table>
  405. <b>Table 2: Relative memory consumption of <code>multi_index_container</code> with 1
  406. sequenced index.</b>
  407. </p>
  408. <p>
  409. The figures confirm that in this case <code>multi_index_container</code> nodes are the
  410. same size than those of its <code>std::list</code> counterpart.
  411. </p>
  412. <h4><a name="time_1s">Execution time</a></h4>
  413. <p align="center">
  414. <img src="perf_1s.png" alt="performance of multi_index_container with 1 sequenced index"
  415. width="556" height="372"><br>
  416. <b>Fig. 2: Performance of <code>multi_index_container</code> with 1 sequenced index.</b>
  417. </p>
  418. <p>
  419. <code>multi_index_container</code> does not attain the performance
  420. of its STL counterpart, although the figures are close. Again, the worst results
  421. are those of GCC, with a degradation of up to 7%, while ICC and MSVC do not
  422. exceed a mere 5%.
  423. </p>
  424. <h3><a name="test_2r">Results for 2 ordered indices</a></h3>
  425. <p>
  426. The following instantiation of <code>multi_index_container</code> was tested:
  427. </p>
  428. <blockquote><pre>
  429. <span class=identifier>multi_index_container</span><span class=special>&lt;</span>
  430. <span class=keyword>int</span><span class=special>,</span>
  431. <span class=identifier>indexed_by</span><span class=special>&lt;</span>
  432. <span class=identifier>ordered_unique</span><span class=special>&lt;</span><span class=identifier>identity</span><span class=special>&lt;</span><span class=keyword>int</span><span class=special>&gt;</span> <span class=special>&gt;,</span>
  433. <span class=identifier>ordered_non_unique</span><span class=special>&lt;</span><span class=identifier>identity</span><span class=special>&lt;</span><span class=keyword>int</span><span class=special>&gt;</span> <span class=special>&gt;</span>
  434. <span class=special>&gt;</span>
  435. <span class=special>&gt;</span>
  436. </pre></blockquote>
  437. <h4><a name="memory_2r">Memory consumption</a></h4>
  438. <p align="center">
  439. <table cellspacing="0">
  440. <tr>
  441. <th width="33%">GCC 3.4.5</th>
  442. <th width="33%">ICC 7.1</th>
  443. <th width="33%">MSVC 8.0</th>
  444. </tr>
  445. <tr>
  446. <td align="center">70%</td>
  447. <td align="center">70%</td>
  448. <td align="center">70%</td>
  449. </tr>
  450. </table>
  451. <b>Table 3: Relative memory consumption of <code>multi_index_container</code> with 2
  452. ordered indices.</b>
  453. </p>
  454. <p>
  455. These results coincide with the theoretical formula for
  456. <i>S<sub>I</sub></i> = 28, <i>N</i> = <i>O</i> = 2 and <i>p</i> = <i>w</i> = 4.
  457. </p>
  458. <h4><a name="time_2r">Execution time</a></h4>
  459. <p align="center">
  460. <img src="perf_2o.png" alt="performance of multi_index_container with 2 ordered indices"
  461. width="556" height="372"><br>
  462. <b>Fig. 3: Performance of <code>multi_index_container</code> with 2 ordered indices.</b>
  463. </p>
  464. <p>
  465. The experimental results confirm our hypothesis that <code>multi_index_container</code>
  466. provides an improvement on execution time by an approximately constant factor,
  467. which in this case lies around 60%. There is no obvious explanation for the
  468. increased advantage of <code>multi_index_container</code> in MSVC for
  469. <i>n</i>=10<sup>5</sup>.
  470. </p>
  471. <h3><a name="test_1r1s">Results for 1 ordered index + 1 sequenced index</a></h3>
  472. <p>
  473. The following instantiation of <code>multi_index_container</code> was tested:
  474. </p>
  475. <blockquote><pre>
  476. <span class=identifier>multi_index_container</span><span class=special>&lt;</span>
  477. <span class=keyword>int</span><span class=special>,</span>
  478. <span class=identifier>indexed_by</span><span class=special>&lt;</span>
  479. <span class=identifier>ordered_unique</span><span class=special>&lt;</span><span class=identifier>identity</span><span class=special>&lt;</span><span class=keyword>int</span><span class=special>&gt;</span> <span class=special>&gt;,</span>
  480. <span class=identifier>sequenced</span><span class=special>&lt;&gt;</span>
  481. <span class=special>&gt;</span>
  482. <span class=special>&gt;</span>
  483. </pre></blockquote>
  484. <h4><a name="memory_1r1s">Memory consumption</a></h4>
  485. <p align="center">
  486. <table cellspacing="0">
  487. <tr>
  488. <th width="33%">GCC 3.4.5</th>
  489. <th width="33%">ICC 7.1</th>
  490. <th width="33%">MSVC 8.0</th>
  491. </tr>
  492. <tr>
  493. <td align="center">75%</td>
  494. <td align="center">75%</td>
  495. <td align="center">75%</td>
  496. </tr>
  497. </table>
  498. <b>Table 4: Relative memory consumption of <code>multi_index_container</code> with 1
  499. ordered index + 1 sequenced index.</b>
  500. </p>
  501. <p>
  502. These results coincide with the theoretical formula for
  503. <i>S<sub>I</sub></i> = 24, <i>N</i> = 2, <i>O</i> = 1 and <i>p</i> = <i>w</i> = 4.
  504. </p>
  505. <h4><a name="time_1r1s">Execution time</a></h4>
  506. <p align="center">
  507. <img src="perf_1o1s.png"
  508. alt="performance of multi_index_container with 1 ordered index + 1 sequenced index"
  509. width="556" height="372"><br>
  510. <b>Fig. 4: Performance of <code>multi_index_container</code> with 1 ordered index
  511. + 1 sequenced index.</b>
  512. </p>
  513. <p>
  514. For <i>n</i>=10<sup>3</sup> and <i>n</i>=10<sup>4</sup>, the results
  515. are in agreement with our theoretical analysis, showing a constant factor
  516. improvement of 50-65% with respect to the STL-based manual simulation.
  517. Curiously enough, this speedup gets even higher when
  518. <i>n</i>=10<sup>5</sup> for two of the compilers, namely GCC and ICC.
  519. In order to rule out spurious results, the tests
  520. have been run many times, yielding similar outcomes. Both test environments
  521. are deployed on the same machine, which points to some OS-related reason for
  522. this phenomenon.
  523. </p>
  524. <h3><a name="test_3r">Results for 3 ordered indices</a></h3>
  525. <p>
  526. The following instantiation of <code>multi_index_container</code> was tested:
  527. </p>
  528. <blockquote><pre>
  529. <span class=identifier>multi_index_container</span><span class=special>&lt;</span>
  530. <span class=keyword>int</span><span class=special>,</span>
  531. <span class=identifier>indexed_by</span><span class=special>&lt;</span>
  532. <span class=identifier>ordered_unique</span><span class=special>&lt;</span><span class=identifier>identity</span><span class=special>&lt;</span><span class=keyword>int</span><span class=special>&gt;</span> <span class=special>&gt;,</span>
  533. <span class=identifier>ordered_non_unique</span><span class=special>&lt;</span><span class=identifier>identity</span><span class=special>&lt;</span><span class=keyword>int</span><span class=special>&gt;</span> <span class=special>&gt;,</span>
  534. <span class=identifier>ordered_non_unique</span><span class=special>&lt;</span><span class=identifier>identity</span><span class=special>&lt;</span><span class=keyword>int</span><span class=special>&gt;</span> <span class=special>&gt;</span>
  535. <span class=special>&gt;</span>
  536. <span class=special>&gt;</span>
  537. </pre></blockquote>
  538. <h4><a name="memory_3r">Memory consumption</a></h4>
  539. <p align="center">
  540. <table cellspacing="0">
  541. <tr>
  542. <th width="33%">GCC 3.4.5</th>
  543. <th width="33%">ICC 7.1</th>
  544. <th width="33%">MSVC 8.0</th>
  545. </tr>
  546. <tr>
  547. <td align="center">66.7%</td>
  548. <td align="center">66.7%</td>
  549. <td align="center">66.7%</td>
  550. </tr>
  551. </table>
  552. <b>Table 5: Relative memory consumption of <code>multi_index_container</code> with 3
  553. ordered indices.</b>
  554. </p>
  555. <p>
  556. These results coincide with the theoretical formula for
  557. <i>S<sub>I</sub></i> = 40, <i>N</i> = <i>O</i> = 3 and <i>p</i> = <i>w</i> = 4.
  558. </p>
  559. <h4><a name="time_3r">Execution time</a></h4>
  560. <p align="center">
  561. <img src="perf_3o.png" alt="performance of multi_index_container with 3 ordered indices"
  562. width="556" height="372"><br>
  563. <b>Fig. 5: Performance of <code>multi_index_container</code> with 3 ordered indices.</b>
  564. </p>
  565. <p>
  566. Execution time for this case is between 45% and 55% lower than achieved with
  567. an STL-based manual simulation of the same data structure.
  568. </p>
  569. <h3><a name="test_2r1s">Results for 2 ordered indices + 1 sequenced index</a></h3>
  570. <p>
  571. The following instantiation of <code>multi_index_container</code> was tested:
  572. </p>
  573. <blockquote><pre>
  574. <span class=identifier>multi_index_container</span><span class=special>&lt;</span>
  575. <span class=keyword>int</span><span class=special>,</span>
  576. <span class=identifier>indexed_by</span><span class=special>&lt;</span>
  577. <span class=identifier>ordered_unique</span><span class=special>&lt;</span><span class=identifier>identity</span><span class=special>&lt;</span><span class=keyword>int</span><span class=special>&gt;</span> <span class=special>&gt;,</span>
  578. <span class=identifier>ordered_non_unique</span><span class=special>&lt;</span><span class=identifier>identity</span><span class=special>&lt;</span><span class=keyword>int</span><span class=special>&gt;</span> <span class=special>&gt;,</span>
  579. <span class=identifier>sequenced</span><span class=special>&lt;&gt;</span>
  580. <span class=special>&gt;</span>
  581. <span class=special>&gt;</span>
  582. </pre></blockquote>
  583. <h4><a name="memory_2r1s">Memory consumption</a></h4>
  584. <p align="center">
  585. <table cellspacing="0">
  586. <tr>
  587. <th width="33%">GCC 3.4.5</th>
  588. <th width="33%">ICC 7.1</th>
  589. <th width="33%">MSVC 8.0</th>
  590. </tr>
  591. <tr>
  592. <td align="center">69.2%</td>
  593. <td align="center">69.2%</td>
  594. <td align="center">69.2%</td>
  595. </tr>
  596. </table>
  597. <b>Table 6: Relative memory consumption of <code>multi_index_container</code> with 2
  598. ordered indices + 1 sequenced index.</b>
  599. </p>
  600. <p>
  601. These results coincide with the theoretical formula for
  602. <i>S<sub>I</sub></i> = 36, <i>N</i> = 3, <i>O</i> = 2 and <i>p</i> = <i>w</i> = 4.
  603. </p>
  604. <h4><a name="time_2r1s">Execution time</a></h4>
  605. <p align="center">
  606. <img src="perf_2o1s.png"
  607. alt="performance of multi_index_container with 2 ordered indices + 1 sequenced index"
  608. width="556" height="372"><br>
  609. <b>Fig. 6: Performance of <code>multi_index_container</code> with 2 ordered indices
  610. + 1 sequenced index.</b>
  611. </p>
  612. <p>
  613. In accordance to the expectations, execution time is improved by a fairly constant
  614. factor, which ranges from 45% to 55%.
  615. </p>
  616. <h2><a name="conclusions">Conclusions</a></h2>
  617. <p>
  618. We have shown that <code>multi_index_container</code> outperforms, both in space and
  619. time efficiency, equivalent data structures obtained from the manual
  620. combination of STL containers. This improvement gets larger when the number
  621. of indices increase.
  622. </p>
  623. <p>
  624. In the special case of replacing standard containers with single-indexed
  625. <code>multi_index_container</code>s, the performance of Boost.MultiIndex
  626. is comparable with that of the tested STL implementations, and can even yield
  627. some improvements both in space consumption and execution time.
  628. </p>
  629. <hr>
  630. <div class="prev_link"><a href="compiler_specifics.html"><img src="prev.gif" alt="compiler specifics" border="0"><br>
  631. Compiler specifics
  632. </a></div>
  633. <div class="up_link"><a href="index.html"><img src="up.gif" alt="index" border="0"><br>
  634. Index
  635. </a></div>
  636. <div class="next_link"><a href="examples.html"><img src="next.gif" alt="examples" border="0"><br>
  637. Examples
  638. </a></div><br clear="all" style="clear: all;">
  639. <br>
  640. <p>Revised November 24th 2015</p>
  641. <p>&copy; Copyright 2003-2015 Joaqu&iacute;n M L&oacute;pez Mu&ntilde;oz.
  642. Distributed under the Boost Software
  643. License, Version 1.0. (See accompanying file <a href="../../../LICENSE_1_0.txt">
  644. LICENSE_1_0.txt</a> or copy at <a href="http://www.boost.org/LICENSE_1_0.txt">
  645. http://www.boost.org/LICENSE_1_0.txt</a>)
  646. </p>
  647. </body>
  648. </html>