编程问题之外的思考

最近在Windows Mobile平台上做一款软件,在解析资讯文本的时候,发现速度奇慢无比,代码是这样写的:

SplitCString(iszString,*strArray,L"\n");

for(int j = 0;j < strArray->GetSize();j++)

{

splitStr = strArray->GetAt(j);

for(int i = 0;i < strArray->GetAt(j).GetLength();i++)

{

STOCKRANGE inforDetailContent;

currentWid++;

textsize = GetDC()->GetTextExtent(splitStr.Left(currentWid));

if(textsize.cx > lineWidth)

{

inforDetailContent.stockCode = splitStr.Left(currentWid);

splitStr = splitStr.Right(splitStr.GetLength()-currentWid);

inforDetailContent.decimalNumber = ++wordNumber;

StockRangeData.push_back(inforDetailContent);

currentWid = 0;

textsize.SetSize(0,0);

continue;

}else if(currentWid == splitStr.GetLength())

{

inforDetailContent.stockCode = splitStr;

inforDetailContent.decimalNumber = ++wordNumber;

StockRangeData.push_back(inforDetailContent);

STOCKRANGE blankContent;

blankContent.stockCode = L"";

blankContent.decimalNumber = ++wordNumber;

StockRangeData.push_back(blankContent);

currentWid = 0;

textsize.SetSize(0,0);

break;

}

}

}

delete strArray;

strArray = NULL;

       作为Windows Mobile开发的人都知道,对绘图句柄进行GetTextExtent()操作是很影响效率的,那么是否有必要对每一个字符遍历并求出字符在设备上占用的尺寸?这和多数人抱怨Android平台的不兼容性是一样的,既然Android平台不同版本之间的兼容性差,那么为什么程序员还要依赖于具体某个平台进行编程呢?Android程序员有义务跨平台进行Android应用程序的开发并保证代码的兼容性。

       那么上面的代码既然知道GetTextExtent()方法的效率低下,是否可以尽量不用或者少用这个方法获取字符的尺寸?看看下面新写的方法:

SplitCString(iszString,*strArray,L"\n");



textsize = GetDC()->GetTextExtent(L"股");

int textNumber = lineWidth/textsize.cx;

for(int j = 0;j < strArray->GetSize();j++)

{

splitStr = strArray->GetAt(j);

int columeNumber = splitStr.GetLength()/textNumber+((splitStr.GetLength()%textNumber)>0?1:0);

if(columeNumber == 0)

{

STOCKRANGE inforDetailContent;

inforDetailContent.stockCode = splitStr;

inforDetailContent.decimalNumber = ++wordNumber;

StockRangeData.push_back(inforDetailContent);



STOCKRANGE blankContent;

blankContent.stockCode = L"";

blankContent.decimalNumber = ++wordNumber;

StockRangeData.push_back(blankContent);

continue;

}else

{

for(int i = 0;i < columeNumber;i++)

{

STOCKRANGE inforDetailContent;

if(splitStr.GetLength() > textNumber)

{

inforDetailContent.stockCode = splitStr.Left(textNumber);

splitStr = splitStr.Right(splitStr.GetLength()-textNumber);

inforDetailContent.decimalNumber = ++wordNumber;

StockRangeData.push_back(inforDetailContent);

}else

{

inforDetailContent.stockCode = splitStr;

inforDetailContent.decimalNumber = ++wordNumber;

StockRangeData.push_back(inforDetailContent);



STOCKRANGE blankContent;

blankContent.stockCode = L"";

blankContent.decimalNumber = ++wordNumber;

StockRangeData.push_back(blankContent);

}

continue;

}

}

}

       在使用新的方法之后,资讯文本内容的解析速度大为提升,或许这并不是技术上的问题,而是愿意不愿意多思考的问题


你可能感兴趣的:(编程,android,mobile,资讯,Windows Mobile)