[Performance] std.conv.toInt is damn fast!
Sean Kelly
sean at f4.ca
Sat Nov 11 20:02:27 PST 2006
pragma wrote:
> Andrey Khropov wrote:
>> Burton Radons wrote:
>> 13) MS VC++ 8.0 (boost 1.33.1::lexical_cast<int>) -
>> 51.700 sec (Yes, it's hard to believe
>> but check yourself!)
>
> <neo>Whoa.</neo>
>
> You'd think that with all that templating going on it would simply
> unroll into a *faster* routine, not something that runs *two whole
> orders of magnitude* slower.
Not if you look at what lexical_cast does:
* Creates a new std::stringstream object.
* Sets a bunch of properties on the stringstream to ensure data is
processed correctly.
* Passes the data to the stringstream via operator<<, which will involve
DMA if the data as a string occupies more than 16 chars (the small
string optimization catches smaller cases).
* Pulls the data back out again via operator>>.
* Checks the stringstream to ensure that no errors occurred and that no
data remains in the stream, which includes processing any trailing
whitespace, etc.
* Returns the new value.
It's clean and works well, but is hardly fast :-)
Sean
More information about the Digitalmars-d
mailing list