I'm using the following method to format a number with commas:
template<class T>
static std::string FormatNumberWithCommas(T value, int numberOfDecimalPlaces = 0)
{
std::stringstream ss;
ss.imbue(std::locale(""));
ss.precision(numberOfDecimalPlaces);
ss << std::fixed << value;
return ss.str();
}
Profiling has show this method to take a significant amount of time relative to other code. Specifically the profiler has identified the line:
ss.imbue(std::locale(""));
And within that I believe it is the std::locale("")
that is taking long. How can I improve the performance of this method? If it requires using something other than stringstream or doing something semi-hacky in this particular method I'm open to it.