The actual answer will be implementation based, but I'm sure std::string s(size, '\0'); works faster.
std::string s; s.resize(size);
According to the documentation for std :: string .
1) The default constructor. Creates an empty string (zero size and unspecified capacity).
The default constructor will create a string with "undefined capacity". I mean, the implementation can freely determine the default capacity, possibly in the range of 10-15 characters (full speculation).
Then, on the next line, you reallocate the memory ( resize ) with the new size , if size greater than the current capacity . This is probably not what you want!
If you really want to find out finally, you can start the profiler in two ways.
Colin basnett
source share