Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Unfortunately I don't have enough rep yet to comment...</p> <p><code>lexical_cast</code> is not primarily slow because it's generic (template lookups happen at compile-time, so virtual function calls or other lookups/dereferences aren't necessary). <code>lexical_cast</code> is, in my opinion, slow, because it builds on C++ iostreams, which are primarily intended for streaming operations and not single conversions, and because <code>lexical_cast</code> must check for and convert iostream error signals. Thus:</p> <ul> <li>a stream object has to be created and destroyed</li> <li>in the string output case above, note that C++ compilers have a hard time avoiding buffer copies (an alternative is to format directly to the output buffer, like <code>sprintf</code> does, though <code>sprintf</code> won't safely handle buffer overruns)</li> <li><code>lexical_cast</code> has to check for <code>stringstream</code> errors (<code>ss.fail()</code>) in order to throw exceptions on conversion failures</li> </ul> <p><code>lexical_cast</code> is nice because (IMO) exceptions allow trapping all errors without extra effort and because it has a uniform prototype. I don't personally see why either of these properties necessitate slow operation (when no conversion errors occur), though I don't know of such C++ functions which are fast (possibly Spirit or boost::xpressive?).</p> <p>Edit: I just found a message mentioning the use of <code>BOOST_LEXICAL_CAST_ASSUME_C_LOCALE</code> to enable an "itoa" optimisation: <a href="http://old.nabble.com/lexical_cast-optimization-td20817583.html" rel="noreferrer">http://old.nabble.com/lexical_cast-optimization-td20817583.html</a>. There's also a linked <a href="http://accu.org/index.php/journals/1375#Boost" rel="noreferrer">article</a> with a bit more detail.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload