【发布时间】:2012-05-24 06:50:00
【问题描述】:
以下代码将std::string 转换为boost::posix_time::ptime。
在分析之后,我发现花在该函数上的大部分时间(大约 90%)都浪费在为 time_input_facet 分配内存上。我不得不承认我不完全理解下面的代码,特别是为什么time_input_facet 必须分配在空闲内存上。
using boost::posix_time;
const ptime StringToPtime(const string &zeitstempel, const string &formatstring)
{
stringstream ss;
time_input_facet* input_facet = new time_input_facet();
ss.imbue(locale(ss.getloc(), input_facet));
input_facet->format(formatstring.c_str());
ss.str(zeitstempel);
ptime timestamp;
ss >> timestamp;
return timestamp;
}
你有什么办法可以摆脱分配吗?
【问题讨论】: