Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r72224 - in trunk: boost libs/conversion
From: antoshkka_at_[hidden]
Date: 2011-05-27 14:19:41


Author: apolukhin
Date: 2011-05-27 14:19:38 EDT (Fri, 27 May 2011)
New Revision: 72224
URL: http://svn.boost.org/trac/boost/changeset/72224

Log:
Fixes #5576, adds performance section to the lexical_cast documentation, fixes misprint in tests/
Text files modified:
   trunk/boost/lexical_cast.hpp | 73 +++++++++++++++++++++++----------------
   trunk/libs/conversion/lexical_cast.htm | 34 +++++++++++++++++-
   trunk/libs/conversion/lexical_cast_test.cpp | 1
   3 files changed, 74 insertions(+), 34 deletions(-)

Modified: trunk/boost/lexical_cast.hpp
==============================================================================
--- trunk/boost/lexical_cast.hpp (original)
+++ trunk/boost/lexical_cast.hpp 2011-05-27 14:19:38 EDT (Fri, 27 May 2011)
@@ -555,6 +555,10 @@
             BOOST_STATIC_ASSERT(!std::numeric_limits<T>::is_signed);
 #endif
 
+ typedef typename Traits::int_type int_type;
+ CharT const czero = lcast_char_constants<CharT>::zero;
+ int_type const zero = Traits::to_int_type(czero);
+
 #ifndef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
             // TODO: use BOOST_NO_STD_LOCALE
             std::locale loc;
@@ -562,47 +566,54 @@
             numpunct const& np = BOOST_USE_FACET(numpunct, loc);
             std::string const& grouping = np.grouping();
             std::string::size_type const grouping_size = grouping.size();
- CharT thousands_sep = grouping_size ? np.thousands_sep() : 0;
- std::string::size_type group = 0; // current group number
- char last_grp_size =
- grouping_size == 0 || grouping[0] <= 0 ? CHAR_MAX : grouping[0];
+
+ if ( grouping_size && grouping[0] > 0 )
+ {
+
 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
             // Check that ulimited group is unreachable:
             BOOST_STATIC_ASSERT(std::numeric_limits<T>::digits10 < CHAR_MAX);
 #endif
+ CharT thousands_sep = np.thousands_sep();
+ std::string::size_type group = 0; // current group number
+ char last_grp_size = grouping[0];
+ char left = last_grp_size;
 
- char left = last_grp_size;
-#endif
-
- typedef typename Traits::int_type int_type;
- CharT const czero = lcast_char_constants<CharT>::zero;
- int_type const zero = Traits::to_int_type(czero);
-
- do
- {
-#ifndef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
- if(left == 0)
+ do
                 {
- ++group;
- if(group < grouping_size)
+ if(left == 0)
                     {
- char const grp_size = grouping[group];
- last_grp_size = grp_size <= 0 ? CHAR_MAX : grp_size;
+ ++group;
+ if(group < grouping_size)
+ {
+ char const grp_size = grouping[group];
+ last_grp_size = grp_size <= 0 ? CHAR_MAX : grp_size;
+ }
+
+ left = last_grp_size;
+ --finish;
+ Traits::assign(*finish, thousands_sep);
                     }
 
- left = last_grp_size;
+ --left;
+
                     --finish;
- Traits::assign(*finish, thousands_sep);
- }
+ int_type const digit = static_cast<int_type>(n % 10U);
+ Traits::assign(*finish, Traits::to_char_type(zero + digit));
+ n /= 10;
+ } while(n);
 
- --left;
+ } else
 #endif
-
- --finish;
- int_type const digit = static_cast<int_type>(n % 10U);
- Traits::assign(*finish, Traits::to_char_type(zero + digit));
- n /= 10;
- } while(n);
+ {
+ do
+ {
+ --finish;
+ int_type const digit = static_cast<int_type>(n % 10U);
+ Traits::assign(*finish, Traits::to_char_type(zero + digit));
+ n /= 10;
+ } while(n);
+ }
 
             return finish;
         }
@@ -638,10 +649,10 @@
             /* According to [22.2.2.1.2] of Programming languages - C++
              * we MUST check for correct grouping
              */
- if (grouping_size)
+ if (grouping_size && grouping[0] > 0)
             {
                 unsigned char current_grouping = 0;
- CharT const thousands_sep = grouping_size ? np.thousands_sep() : 0;
+ CharT const thousands_sep = np.thousands_sep();
                 char remained = grouping[current_grouping] - 1;
 
                 for(;end>=begin; --end)

Modified: trunk/libs/conversion/lexical_cast.htm
==============================================================================
--- trunk/libs/conversion/lexical_cast.htm (original)
+++ trunk/libs/conversion/lexical_cast.htm 2011-05-27 14:19:38 EDT (Fri, 27 May 2011)
@@ -26,6 +26,8 @@
                                 <a href="#references">References</a></li>
                         <li>
                                 <a href="#changes">Changes</a></li>
+ <li>
+ Performance</li>
                 </ul>
                 <hr>
                 <h2><a name="motivation">Motivation</a></h2>
@@ -87,7 +89,7 @@
                         For a good discussion of the options and issues involved in string-based
                         formatting, including comparison of <code>stringstream</code>, <code>lexical_cast</code>,
                         and others, see Herb Sutter's article, <a href="http://www.gotw.ca/publications/mill19.htm">
- <i>The String Formatters of Manor Farm</i></a>.
+ <i>The String Formatters of Manor Farm</i></a>. Also, take a look at the Performance section.
                 <p>
                         <hr>
                         <h2><a name="examples">Examples</a></h2>
@@ -267,6 +269,7 @@
 <h2><a name="changes">Changes</a></h2>
 <h3>May 2011:</h3>
 <ul type="square">
+ <li>Optimizations for "C" and other locales without number grouping.</li>
     <li>Better performance and less memory usage for unsigned char and signed char conversions.</li>
     <li>Better performance and less memory usage for conversions to arithmetic types.</li>
     <li>Better performance and less memory usage for conversions from arithmetic type to arithmetic type.</li>
@@ -313,7 +316,34 @@
 </ul>
         <p>
                         <hr>
-
+
+<h2><a name="performance">Performance</a></h2>
+This table shows the execution speed for 100000 iterations in milliseconds of the following code blocks:
+
+<table border="1" width="100%">
+ <tr>
+ <td>From->To</td><td> <code>target = lexical_cast&lt;Target&gt;(source);</code> </td>
+ <td><code>std::stringstream ss;<br>ss &lt;&lt; source;<br> ss &gt;&gt; target;</code></td>
+ <td><code>ss &lt;&lt; source;<br> ss &gt;&gt; target;<br>ss.str(std::string());<br>ss.clear();</code></td>
+ <td><code>sscanf( (const char*)source, <...>, &amp;target);</code> <br>OR<br> <code>sprintf( (char*)buffer, <...>, source);<br>
+ target = buffer;</code></td></tr>
+ <tr><td>string->char</td><td bgcolor="#00C000"><1</td><td>91</td><td>7</td><td>10</td></tr>
+ <tr><td>string->int</td><td bgcolor="#00C000">7</td><td>115</td><td>23</td><td>18</td></tr>
+ <tr><td>string->unsigned int</td><td bgcolor="#00C000">7</td><td>117</td><td>22</td><td>17</td></tr>
+ <tr><td>string->bool</td><td bgcolor="#00C000"><1</td><td>104</td><td>19</td><td>10</td></tr>
+ <tr><td>string->float</td><td>85</td><td>172</td><td>60</td><td bgcolor="#00C000">33</td></tr>
+ <tr><td>char->string</td><td bgcolor="#00C000">7</td><td>105</td><td>16</td><td>12</td></tr>
+ <tr><td>int->string</td><td bgcolor="#00C000">15</td><td>131</td><td>21</td><td>17</td></tr>
+ <tr><td>unsigned int->string</td><td bgcolor="#00C000">14</td><td>125</td><td>21</td><td>17</td></tr>
+ <tr><td>bool->string</td><td bgcolor="#00C000">7</td><td>122</td><td>24</td><td>12</td></tr>
+ <tr><td>float->string</td><td>124</td><td>223</td><td>115</td><td bgcolor="#00C000">48</td></tr>
+ <tr><td>char*->string</td><td bgcolor="#00C000">9</td><td>123</td><td>20</td><td>---</td></tr>
+ <tr><td>int->int</td><td bgcolor="#00C000"><1</td><td>120</td><td>26</td><td>---</td></tr>
+ <tr><td>float->float</td><td bgcolor="#00C000"><1</td><td>262</td><td>142</td><td>---</td></tr>
+</table>
+
+Fastest results are highlitened with green.
+ <hr>
 <div align="right"><small><i>Copyright &copy; Kevlin Henney, 2000-2005</i></small></div>
 <div align="right"><small><i>Copyright &copy; Alexander Nasonov, 2006-2010</i></small></div>
 <div align="right"><small><i>Copyright &copy; Antony Polukhin, 2011</i></small></div>

Modified: trunk/libs/conversion/lexical_cast_test.cpp
==============================================================================
--- trunk/libs/conversion/lexical_cast_test.cpp (original)
+++ trunk/libs/conversion/lexical_cast_test.cpp 2011-05-27 14:19:38 EDT (Fri, 27 May 2011)
@@ -937,7 +937,6 @@
 
 #endif
 
-#include <iostream>
 void test_char_types_conversions()
 {
     const char c_arr[] = "Test array of chars";


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk