[英]CComSafeArray to Tab-Separated Values Suggestions for Speed Improvement
我需要將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;
}
謝謝@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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.