• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>The Bigger Picture</title>
5<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7<link rel="home" href="../../index.html" title="Chapter 1. Boost.Convert 2.0">
8<link rel="up" href="../performance.html" title="Performance">
9<link rel="prev" href="boost_convert_overhead.html" title="Boost.Convert Overhead">
10<link rel="next" href="../other_conversions.html" title="Beyond Basic Conversions">
11</head>
12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13<table cellpadding="2" width="100%"><tr>
14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
15<td align="center"><a href="../../../../../../index.html">Home</a></td>
16<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
19<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
20</tr></table>
21<hr>
22<div class="spirit-nav">
23<a accesskey="p" href="boost_convert_overhead.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../performance.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../other_conversions.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h3 class="title">
27<a name="boost_convert.performance.the_bigger_picture"></a><a class="link" href="the_bigger_picture.html" title="The Bigger Picture">The Bigger
28      Picture</a>
29</h3></div></div></div>
30<p>
31        The presented conversion-related performance data (and potential performance-related
32        concerns) have to be taken in perspective. A special project with serious
33        high performance requirements is most likely to require special solutions
34        which are most likely to cast out many generic libraries. Still, the existence
35        of such projects did not kill or diminish in any way the importance of generic
36        libraries. And <span class="emphasis"><em>Boost.Convert</em></span> is one such <span class="emphasis"><em>generic</em></span>
37        library.
38      </p>
39<p>
40        More so, if performance is a concern, then, in all likelihood, for the great
41        majority of the mainstream projects <span class="emphasis"><em>Boost.Convert</em></span> will
42        be far from the top on the performance profiler's list. For example, to measure
43        conversion-related overhead relative to <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span></code>-related
44        overhead, <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span></code> was replaced with a custom <span class="emphasis"><em>small-string</em></span>
45        type:
46      </p>
47<p>
48</p>
49<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">my_string</span>
50<span class="special">{</span>
51    <span class="keyword">typedef</span> <span class="identifier">my_string</span>              <span class="identifier">this_type</span><span class="special">;</span>
52    <span class="keyword">typedef</span> <span class="keyword">char</span>                  <span class="identifier">value_type</span><span class="special">;</span>
53    <span class="keyword">typedef</span> <span class="identifier">value_type</span><span class="special">*</span>             <span class="identifier">iterator</span><span class="special">;</span>
54    <span class="keyword">typedef</span> <span class="identifier">value_type</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">const_iterator</span><span class="special">;</span>
55
56    <span class="identifier">my_string</span> <span class="special">();</span>
57    <span class="identifier">my_string</span> <span class="special">(</span><span class="identifier">const_iterator</span><span class="special">,</span> <span class="identifier">const_iterator</span> <span class="special">=</span><span class="number">0</span><span class="special">);</span>
58
59    <span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span>    <span class="identifier">c_str</span> <span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">storage_</span><span class="special">;</span> <span class="special">}</span>
60    <span class="identifier">const_iterator</span> <span class="identifier">begin</span> <span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">storage_</span><span class="special">;</span> <span class="special">}</span>
61    <span class="identifier">const_iterator</span>   <span class="identifier">end</span> <span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">storage_</span> <span class="special">+</span> <span class="identifier">strlen</span><span class="special">(</span><span class="identifier">storage_</span><span class="special">);</span> <span class="special">}</span>
62    <span class="identifier">this_type</span><span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=</span> <span class="special">(</span><span class="keyword">char</span> <span class="keyword">const</span><span class="special">*);</span>
63
64    <span class="keyword">private</span><span class="special">:</span>
65
66    <span class="keyword">static</span> <span class="identifier">size_t</span> <span class="keyword">const</span> <span class="identifier">size_</span> <span class="special">=</span> <span class="number">12</span><span class="special">;</span>
67    <span class="keyword">char</span> <span class="identifier">storage_</span><span class="special">[</span><span class="identifier">size_</span><span class="special">];</span>
68<span class="special">};</span>
69</pre>
70<p>
71      </p>
72<p>
73        The class above is essentially a glorified 12-bytes character buffer sufficient
74        to accommodate string representations of 32-bit integers. Deployed with the
75        same converters and the same algorithms:
76      </p>
77<p>
78</p>
79<pre class="programlisting"><span class="identifier">printf</span><span class="special">(</span><span class="string">"strtol int-to std::string/small-string: %.2f/%.2f seconds.\n"</span><span class="special">,</span>
80       <span class="identifier">local</span><span class="special">::</span><span class="identifier">to_str</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">,</span> <span class="keyword">int</span><span class="special">&gt;(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">::</span><span class="identifier">strtol</span><span class="special">()),</span>
81       <span class="identifier">local</span><span class="special">::</span><span class="identifier">to_str</span><span class="special">&lt;</span>  <span class="identifier">my_string</span><span class="special">,</span> <span class="keyword">int</span><span class="special">&gt;(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">::</span><span class="identifier">strtol</span><span class="special">()));</span>
82<span class="identifier">printf</span><span class="special">(</span><span class="string">"spirit int-to std::string/small-string: %.2f/%.2f seconds.\n"</span><span class="special">,</span>
83       <span class="identifier">local</span><span class="special">::</span><span class="identifier">to_str</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">,</span> <span class="keyword">int</span><span class="special">&gt;(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">()),</span>
84       <span class="identifier">local</span><span class="special">::</span><span class="identifier">to_str</span><span class="special">&lt;</span>  <span class="identifier">my_string</span><span class="special">,</span> <span class="keyword">int</span><span class="special">&gt;(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">()));</span>
85<span class="identifier">printf</span><span class="special">(</span><span class="string">"stream int-to std::string/small-string: %.2f/%.2f seconds.\n"</span><span class="special">,</span>
86       <span class="identifier">local</span><span class="special">::</span><span class="identifier">to_str</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">,</span> <span class="keyword">int</span><span class="special">&gt;(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">::</span><span class="identifier">cstream</span><span class="special">()),</span>
87       <span class="identifier">local</span><span class="special">::</span><span class="identifier">to_str</span><span class="special">&lt;</span>  <span class="identifier">my_string</span><span class="special">,</span> <span class="keyword">int</span><span class="special">&gt;(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">::</span><span class="identifier">cstream</span><span class="special">()));</span>
88</pre>
89<p>
90      </p>
91<p>
92        It produced the following relative results (compiled with <code class="computeroutput"><span class="identifier">gcc</span>
93        <span class="special">-</span><span class="identifier">O3</span></code>):
94      </p>
95<pre class="programlisting"><span class="identifier">strtol</span> <span class="keyword">int</span><span class="special">-</span><span class="identifier">to</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">/</span><span class="identifier">small</span><span class="special">-</span><span class="identifier">string</span><span class="special">:</span> <span class="number">1.41</span><span class="special">/</span><span class="number">0.53</span> <span class="identifier">seconds</span><span class="special">.</span>
96<span class="identifier">spirit</span> <span class="keyword">int</span><span class="special">-</span><span class="identifier">to</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">/</span><span class="identifier">small</span><span class="special">-</span><span class="identifier">string</span><span class="special">:</span> <span class="number">1.98</span><span class="special">/</span><span class="number">1.04</span> <span class="identifier">seconds</span><span class="special">.</span>
97<span class="identifier">stream</span> <span class="keyword">int</span><span class="special">-</span><span class="identifier">to</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">/</span><span class="identifier">small</span><span class="special">-</span><span class="identifier">string</span><span class="special">:</span> <span class="number">2.49</span><span class="special">/</span><span class="number">1.40</span> <span class="identifier">seconds</span><span class="special">.</span>
98</pre>
99<p>
100        The results suggest that for the projects that deal with string-related conversions
101        and routinely deploy <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span></code>
102        facilities the conversion-related overhead will be outweighed by the overhead
103        of <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span></code>-related operations.
104      </p>
105</div>
106<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
107<td align="left"></td>
108<td align="right"><div class="copyright-footer">Copyright © 2009-2016 Vladimir Batov<p>
109        Distributed under the Boost Software License, Version 1.0. See copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>.
110      </p>
111</div></td>
112</tr></table>
113<hr>
114<div class="spirit-nav">
115<a accesskey="p" href="boost_convert_overhead.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../performance.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../other_conversions.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
116</div>
117</body>
118</html>
119