我需要将2D CComSafe数组转换为制表符分隔的值文本流。该数组可以包含任意数量的值,可能为数百万。
相关的代码块如下。
我强烈怀疑generate_response_from_data函数只是生成输出的一种残酷方式。但是我还没有找到更好方法的具体例子。是的,我已经尽力了。
我试图弄清楚Boost Karma是否会是更好的解决方案,但是坦率地说,我只是想不出如何将其应用于我的用例。
有人可以提供一些有关更快方法的输入吗?
// This is a 2D CComSafeArray
template<typename T>
class MyDataArray : public CComSafeArray<T>
{
public:
MyDataArray() : CComSafeArray<T>() {}
const T* get_value_ptr(long row, long col) const // 0-based indices.
{
// To shave off a tiny bit of time, validity of m_psa, row, and col are assumed.
// Not great but for our application, those are checked prior to call.
return &static_cast<T*>(this->m_psa->pvData)[this->m_psa->rgsabound[1].cElements * col + row];
}
// Other stuff for this class.
};
inline std::string my_variant_to_string(const VARIANT* p_var)
{
// Will only ever have VT_I4, VT_R8, VT_BSTR
if (VT_I4 == p_var->vt)
return boost::lexical_cast<std::string>(p_var->intVal); // Boost faster than other methods!!!
if (VT_R8 == p_var->vt)
return boost::lexical_cast<std::string>(p_var->dblVal); // Boost faster than other methods!!!
if (VT_BSTR == p_var->vt)
{
std::wstring wstr(p_var->bstrVal, SysStringLen(p_var->bstrVal));
return Utils::from_wide(wstr); // from_wide is a conversion function I created.
}
//if (VT_EMPTY == == p_var->vt) {} // Technically not needed.
return "";
}
template<typename T>
bool generate_response_from_data(const MyDataArray<T>& data_array, std::stringstream& response_body)
{
if (2 != data_array.GetDimensions())
return false;
long row_begin = data_array.GetLowerBound(0);
long row_end = data_array.GetUpperBound(0);
long col_begin = data_array.GetLowerBound(1);
long col_end = data_array.GetUpperBound(1);
if (row_end < row_begin || col_end < col_begin)
return false;
for (long r = row_begin; r <= row_end; ++r)
{
for (long c = col_begin; c <= col_end; ++c)
{
if (c > 0)
response_body << '\t';
response_body << my_variant_to_string(data_array.get_value_ptr(r, c));
}
response_body << '\n';
}
return true;
}
答案 0 :(得分:0)
感谢@MichaelGunter。您对过载的建议<<可使速度提高〜60%!我从wstring转换为每个值,因为与数字值相比,字符串值的百分比很小(如果有的话),在大多数情况下转换整个流可能是浪费的。
经过编辑以反映使用自定义功能从BSTR直接转换为std :: string。
std::stringstream& operator<<(std::stringstream& s, const VARIANT* p_v)
{
if (VT_I4 == p_v->vt)
s << p_v->intVal;
else if (VT_R8 == p_v->vt)
s << p_v->dblVal;
else if (VT_BSTR == p_v->vt)
{
//std::wstring wstr(p_v->bstrVal, SysStringLen(p_v->bstrVal));
s << Utils::from_bstr(p_v->bstrVal);
}
return s;
}
...
response_body << odata_array.get_value_ptr(r, c);