title: Comparing 3 modern c++ ways to convert integral values to strings
body: <p>I was trying to pick a <a href="http://codexpert.ro/blog/2014/04/14/standard-way-of-converting-between-numbers-and-strings-in-cpp11/" rel="nofollow noreferrer">standard way to convert integrals to strings</a>, so I went on and did a small <strong>performance evaluation</strong> by <a href="https://stackoverflow.com/a/21995693/2567683">measuring the execution time of 3 methods</a></p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;sstream&gt;
  #include &lt;vector&gt;
  #include &lt;chrono&gt;
  #include &lt;random&gt;
  #include &lt;exception&gt;
  #include &lt;type_traits&gt;
  #include &lt;boost/lexical_cast.hpp&gt;
  
  using namespace std;
  
  // 1. A way to easily measure elapsed time -------------------
  template&lt;typename TimeT = std::chrono::milliseconds&gt;
  struct measure
  {
      template&lt;typename F&gt;
      static typename TimeT::rep execution(F const &amp;func)
      {
          auto start = std::chrono::system_clock::now();
          func();
          auto duration = std::chrono::duration_cast&lt; TimeT&gt;(
              std::chrono::system_clock::now() - start);
          return duration.count();
      }
  };
  // -----------------------------------------------------------
  
  // 2. Define the conversion functions ========================
  template&lt;typename T&gt; // A. Using stringstream ================
  string StringFromNumber_SS(T const &amp;value) {
      stringstream ss;
      ss &lt;&lt; value;
      return ss.str();
  }
  
  template&lt;typename T&gt; // B. Using boost::lexical_cast =========
  string StringFromNumber_LC(T const &amp;value) {
      return boost::lexical_cast&lt;string&gt;(value);
  }
  
  template&lt;typename T&gt; // C. Using c++11 to_string() ===========
  string StringFromNumber_C11(T const &amp;value) {
      return std::to_string(value);
  }
  // ===========================================================
  
  // 3. A wrapper to measure the different executions ----------
  template&lt;typename T, typename F&gt;
  long long MeasureExec(std::vector&lt;T&gt; const &amp;v1, F const &amp;func)
  {
      return measure&lt;&gt;::execution([&amp;]() {
          for (auto const &amp;i : v1) {
              if (func(i) != StringFromNumber_LC(i)) {
                  throw std::runtime_error(&quot;FAIL&quot;);
              }
          }
      });
  }
  // -----------------------------------------------------------
  
  // 4. Machinery to generate random numbers into a vector -----
  template&lt;typename T&gt;
  typename std::enable_if&lt;std::is_integral&lt;T&gt;::value&gt;::type 
  FillVec(vector&lt;T&gt; &amp;v)
  {
      std::mt19937 e2(1);
      std::uniform_int_distribution&lt;&gt; dist(3, 1440);
      std::generate(v.begin(), v.end(), [&amp;]() { return dist(e2); });
  }
  
  template&lt;typename T&gt;
  typename std::enable_if&lt;!std::is_integral&lt;T&gt;::value&gt;::type 
  FillVec(vector&lt;T&gt; &amp;v)
  {
      std::mt19937 e2(1);
      std::uniform_real_distribution&lt;&gt; dist(-1440., 1440.);
      std::generate(v.begin(), v.end(), [&amp;]() { return dist(e2); });
  }
  // -----------------------------------------------------------
  
  int main()
  {
      std::vector&lt;int&gt; v1(991908);
      FillVec(v1);
  
      cout &lt;&lt; &quot;C++ 11 method ......... &quot; &lt;&lt;
          MeasureExec(v1, StringFromNumber_C11&lt;int&gt;) &lt;&lt; endl;
      cout &lt;&lt; &quot;String stream method .. &quot; &lt;&lt;
          MeasureExec(v1, StringFromNumber_SS&lt;int&gt;) &lt;&lt; endl;
      cout &lt;&lt; &quot;Lexical cast method ... &quot; &lt;&lt;
          MeasureExec(v1, StringFromNumber_LC&lt;int&gt;) &lt;&lt; endl;
  
      return 0;
  }
  </code></pre>
  <p>A <strong>typical output</strong> (running Release in VS2013 which implies /O2 optimization flag) would be</p>
  <blockquote>
  <p>C++ 11 method ......... 273</p>
  </blockquote>
  <blockquote>
  <p>String stream method .. 1923</p>
  </blockquote>
  <blockquote>
  <p>Lexical cast method ... 222</p>
  </blockquote>
  <h2>UPDATE</h2>
  <p>Alternatively an <a href="http://coliru.stacked-crooked.com/a/af7453e6b3d57ce1" rel="nofollow noreferrer">online run on gcc</a> with</p>
  <pre><code>g++ -std=c++11 -Ofast -march=native -Wall -pedantic main.cpp &amp;&amp; ./a.out
  </code></pre>
  <blockquote>
  <p>C++ 11 method ......... 414</p>
  </blockquote>
  <blockquote>
  <p>String stream method .. 1538</p>
  </blockquote>
  <blockquote>
  <p>Lexical cast method ... 275</p>
  </blockquote>
  <p><em>Disclaimer : Results are to be compared among each other and not across machines</em></p>
  <h2>Questions</h2>
  <p><strong>1. Why is the string stream method consistently the worst (by an order of magnitude)? Should it be viewed as deprecated now that faster alternatives emerged?</strong></p>
  <p><strong>2. Why is lexical cast consistently the best? Can we assume that this is the fastest implementation?</strong></p>
  <p>Please feel free to tweak and play with your versions of this code. I'd appreciate your insights on the topic.</p>
  <h2>PS</h2>
  <p>The code that was actually run, had only one measurement per <code>main()</code>. Here all were 3 were presented together to save space.</p>
  <p>Optimization flags are compiler specific or application mandated. I'm just providing the code blocks to perform the tests and expect from SO users to chip in with their results or suggestions to what the optimum configuration per compiler would be (for what it's worth I provided the flags used here).</p>
  <p><strong>The code works for any numeric to string conversion</strong> (it takes changing the type of <code>v1</code> in <code>main</code>). sehe did for <code>double</code> (mentioned in his answer's comment). It's a good idea to play with that too.</p>