计算机科学与技术 外文翻译 外文文献 英文文献 提高字符串处理性能的应用程序Word下载.docx

上传人:b****2 文档编号:3663748 上传时间:2023-05-02 格式:DOCX 页数:28 大小:49.13KB
下载 相关 举报
计算机科学与技术 外文翻译 外文文献 英文文献 提高字符串处理性能的应用程序Word下载.docx_第1页
第1页 / 共28页
计算机科学与技术 外文翻译 外文文献 英文文献 提高字符串处理性能的应用程序Word下载.docx_第2页
第2页 / 共28页
计算机科学与技术 外文翻译 外文文献 英文文献 提高字符串处理性能的应用程序Word下载.docx_第3页
第3页 / 共28页
计算机科学与技术 外文翻译 外文文献 英文文献 提高字符串处理性能的应用程序Word下载.docx_第4页
第4页 / 共28页
计算机科学与技术 外文翻译 外文文献 英文文献 提高字符串处理性能的应用程序Word下载.docx_第5页
第5页 / 共28页
计算机科学与技术 外文翻译 外文文献 英文文献 提高字符串处理性能的应用程序Word下载.docx_第6页
第6页 / 共28页
计算机科学与技术 外文翻译 外文文献 英文文献 提高字符串处理性能的应用程序Word下载.docx_第7页
第7页 / 共28页
计算机科学与技术 外文翻译 外文文献 英文文献 提高字符串处理性能的应用程序Word下载.docx_第8页
第8页 / 共28页
计算机科学与技术 外文翻译 外文文献 英文文献 提高字符串处理性能的应用程序Word下载.docx_第9页
第9页 / 共28页
计算机科学与技术 外文翻译 外文文献 英文文献 提高字符串处理性能的应用程序Word下载.docx_第10页
第10页 / 共28页
计算机科学与技术 外文翻译 外文文献 英文文献 提高字符串处理性能的应用程序Word下载.docx_第11页
第11页 / 共28页
计算机科学与技术 外文翻译 外文文献 英文文献 提高字符串处理性能的应用程序Word下载.docx_第12页
第12页 / 共28页
计算机科学与技术 外文翻译 外文文献 英文文献 提高字符串处理性能的应用程序Word下载.docx_第13页
第13页 / 共28页
计算机科学与技术 外文翻译 外文文献 英文文献 提高字符串处理性能的应用程序Word下载.docx_第14页
第14页 / 共28页
计算机科学与技术 外文翻译 外文文献 英文文献 提高字符串处理性能的应用程序Word下载.docx_第15页
第15页 / 共28页
计算机科学与技术 外文翻译 外文文献 英文文献 提高字符串处理性能的应用程序Word下载.docx_第16页
第16页 / 共28页
计算机科学与技术 外文翻译 外文文献 英文文献 提高字符串处理性能的应用程序Word下载.docx_第17页
第17页 / 共28页
计算机科学与技术 外文翻译 外文文献 英文文献 提高字符串处理性能的应用程序Word下载.docx_第18页
第18页 / 共28页
计算机科学与技术 外文翻译 外文文献 英文文献 提高字符串处理性能的应用程序Word下载.docx_第19页
第19页 / 共28页
计算机科学与技术 外文翻译 外文文献 英文文献 提高字符串处理性能的应用程序Word下载.docx_第20页
第20页 / 共28页
亲,该文档总共28页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

计算机科学与技术 外文翻译 外文文献 英文文献 提高字符串处理性能的应用程序Word下载.docx

《计算机科学与技术 外文翻译 外文文献 英文文献 提高字符串处理性能的应用程序Word下载.docx》由会员分享,可在线阅读,更多相关《计算机科学与技术 外文翻译 外文文献 英文文献 提高字符串处理性能的应用程序Word下载.docx(28页珍藏版)》请在冰点文库上搜索。

计算机科学与技术 外文翻译 外文文献 英文文献 提高字符串处理性能的应用程序Word下载.docx

Summary:

MostActiveServerPages(ASP)applicationsrelyonstringconcatenationtobuildHTML-formatteddatathatisthenpresentedtousers.ThisarticlecontainsacomparisonofseveralwaystocreatethisHTMLdatastream,someofwhichprovidebetterperformancethanothersforagivensituation.AreasonableknowledgeofASPandVisualBasicprogrammingisassumed.(11printedpages)

Contents

Introduction

ASPDesign

StringConcatenation

TheQuickandEasySolution

TheStringBuilder

TheBuilt-inMethod

Testing

Results

Conclusion

WhenwritingASPpages,thedeveloperisreallyjustcreatingastreamofformattedtextthatiswrittentotheWebclientviatheResponseobjectprovidedbyASP.YoucanbuildthistextstreaminseveraldifferentwaysandthemethodyouchoosecanhavealargeimpactonboththeperformanceandthescalabilityoftheWebapplication.OnnumerousoccasionsinwhichIhavehelpedcustomerswithperformance-tuningtheirWebapplications,IhavefoundthatoneofthemajorwinshascomefromchangingthewaythattheHTMLstreamiscreated.InthisarticleIwillshowafewofthecommontechniquesandtestwhateffecttheyhaveontheperformanceofasimpleASPpage.

ManyASPdevelopershavefollowedgoodsoftwareengineeringprinciplesandmodularizedtheircodewhereverpossible.Thisdesignnormallytakestheformofanumberofincludefilesthatcontainfunctionsmodelingparticulardiscretesectionsofapage.Thestringoutputsfromthesefunctions,usuallyHTMLtablecode,canthenbeusedinvariouscombinationstobuildacompletepage.SomedevelopershavetakenthisastagefurtherandmovedtheseHTMLfunctionsintoVisualBasicCOMcomponents,hopingtobenefitfromtheextraperformancethatcompiledcodecanoffer.

Althoughthisiscertainlyagooddesignpractice,themethodusedtobuildthestringsthatformthesediscreteHTMLcodecomponentscanhavealargebearingonhowwelltheWebsiteperformsandscales—regardlessofwhethertheactualoperationisperformedfromwithinanASPincludefileoraVisualBasicCOMcomponent.

ConsiderthefollowingcodefragmenttakenfromafunctioncalledWriteHTML.TheparameternamedDataissimplyanarrayofstringscontainingsomedatathatneedstobeformattedintoatablestructure(datareturnedfromadatabase,forinstance).

CopyCode

FunctionWriteHTML(Data)

DimnRep

FornRep=0to99

sHTML=sHTML&

vbcrlf_

&

"

<

TR>

TD>

"

(nRep+1)&

/TD>

_

Data(0,nRep)&

Data(1,nRep)&

Data(2,nRep)&

Data(3,nRep)&

Data(4,nRep)&

Data(5,nRep)&

/TR>

Next

WriteHTML=sHTML

EndFunction

ThisistypicalofhowmanyASPandVisualBasicdevelopersbuildHTMLcode.ThetextcontainedinthesHTMLvariableisreturnedtothecallingcodeandthenwrittentotheclientusingResponse.Write.Ofcourse,thiscouldalsobeexpressedassimilarcodeembeddeddirectlywithinthepagewithouttheindirectionoftheWriteHTMLfunction.TheproblemwiththiscodeliesinthefactthatthestringdatatypeusedbyASPandVisualBasic,theBSTRorBasicString,cannotactuallychangelength.Thismeansthateverytimethelengthofthestringischanged,theoriginalrepresentationofthestringinmemoryisdestroyed,andanewoneiscreatedcontainingthenewstringdata:

thisresultsinamemoryallocationoperationandamemoryde-allocationoperation.Ofcourse,inASPandVisualBasicthisisalltakencareofforyou,sothetruecostisnotimmediatelyapparent.Allocatingandde-allocatingmemoryrequirestheunderlyingruntimecodetotakeoutexclusivelocksandthereforecanbeexpensive.Thisisespeciallyapparentwhenstringsgetbigandlargeblocksofmemoryarebeingallocatedandde-allocatedinquicksuccession,ashappensduringheavystringconcatenation.Whilethismaypresentnomajorproblemsinasingleuserenvironment,itcancauseseriousperformanceandscalabilityissueswhenusedinaserverenvironmentsuchasinanASPapplicationrunningonaWebserver.

Sobacktothecodefragmentabove:

howmanystringallocationsarebeingperformedhere?

Infacttheansweris16.Inthissituationeveryapplicationofthe'

&

'

operatorcausesthestringpointedtobythevariablesHTMLtobedestroyedandrecreated.Ihavealreadymentionedthatstringallocationisexpensive,becomingincreasinglymoresoasthestringgrows;

armedwiththisknowledge,wecanimproveuponthecodeabove.

Therearetwowaystomitigatetheeffectofstringconcatenations,thefirstistotryanddecreasethesizeofthestringsbeingmanipulatedandthesecondistotryandreducethenumberofstringallocationoperationsbeingperformed.LookattherevisedversionoftheWriteHTMLcodeshownbelow.

(vbcrlf_

Atfirstglanceitmaybedifficulttospotthedifferencebetweenthispieceofcodeandtheprevioussample.ThisonesimplyhastheadditionofparenthesesaroundeverythingaftersHTML=sHTML&

.Thisactuallyreducesthesizeofstringsbeingmanipulatedinmostofthestringconcatenationoperationsbychangingtheorderofprecedence.IntheoriginalcodesampletheASPcomplierwilllookattheexpressiontotherightoftheequalssignandjustevaluateitfromlefttoright.Thisresultsin16concatenationoperationsperiterationinvolvingsHTMLwhichisgrowingallthetime.Inthenewversionwearegivingthecompilerahintbychangingtheorderinwhichitshouldcarryouttheoperations.Nowitwillevaluatetheexpressionfromlefttorightbutalsoinsideout,i.e.insidetheparenthesesfirst.Thistechniqueresultsin15concatenationoperationsperiterationinvolvingsmallerstringswhicharenotgrowingandonlyonewiththelarge,andgrowing,sHTML.Figure1showsanimpressionofthememoryusagepatternsofthisoptimizationagainstthestandardconcatenationmethod.

Figure1 

 

Comparisonofmemoryusagepatternbetweenstandardandparenthesizedconcatenation

Usingparenthesescanmakequiteamarkeddifferenceinperformanceandscalabilityincertaincircumstances,asIwilldemonstratelaterinthisarticle.

Wehaveseenthequickandeasysolutiontothestringconcatenationproblem,andformanysituationsthismayprovidethebesttrade-offbetweenperformanceandefforttoimplement.Ifwewanttogetseriousaboutimprovingtheperformanceofbuildinglargestrings,however,thenweneedtotakethesecondalternative,whichistocutdownthenumberofstringallocationoperations.InordertoachievethisaStringBuilderisrequired.Thisisaclassthatmaintainsaconfigurablestringbufferandmanagesinsertionsofnewpiecesoftextintothatbuffer,causingstringreallocationonlywhenthelengthofthetextexceedsthelengthofthestringbuffer.TheMicrosoft.NETframeworkprovidessuchaclassforfree(System.Text.StringBuilder)thatisrecommendedforallstringconcatenationoperationsinthatenvironment.IntheASPandclassicVisualBasicworldwedonothaveaccesstothisclass,soweneedtobuildourown.BelowisasampleStringBuilderclasscreatedusingVisualBasic6.0(error-handlingcodehasbeenomittedintheinterestofbrevity).

OptionExplicit

defaultinitialsizeofbufferandgrowthfactor

PrivateConstDEF_INITIALSIZEAsLong=1000

PrivateConstDEF_GROWTHAsLong=1000

buffersizeandgrowth

Privatem_nInitialSizeAsLong

Privatem_nGrowthAsLong

bufferandbuffercounters

Privatem_sTextAsString

Privatem_nSizeAsLong

Privatem_nPosAsLong

PrivateSubClass_Initialize()

'

setdefaultsforsizeandgrowth

m_nInitialSize=DEF_INITIALSIZE

m_nGrowth=DEF_GROWTH

initializebuffer

InitBuffer

EndSub

settheinitialsizeandgrowthamount

PublicSubInit(ByValInitialSizeAsLong,ByValGrowthAsLong)

IfInitialSize>

0Thenm_nInitialSize=InitialSize

IfGrowth>

0Thenm_nGrowth=Growth

initializethebuffer

PrivateSubInitBuffer()

m_nSize=-1

m_nPos=1

growthebuffer

PrivateSubGrow(OptionalMinimimGrowthAsLong)

initializebufferifnecessary

Ifm_nSize=-1Then

m_nSize=m_nInitialSize

m_sText=Space$(m_nInitialSize)

Else

justgrow

DimnGrowthAsLong

nGrowth=IIf(m_nGrowth>

MinimimGrowth,

m_nGrowth,MinimimGrowth)

m_nSize=m_nSize+nGrowth

m_sText=m_sText&

Space$(nGrowth)

EndIf

trimthebuffertothecurrentlyusedsize

PrivateSubShrink()

Ifm_nSize>

m_nPosThen

m_nSize=m_nPos-1

m_sText=RTrim$(m_sText)

addasingletextstring

PrivateSubAppendInternal(ByValTextAsString)

If(m_nPos+Len(Text))>

m_nSizeThenGrowLen(Text)

Mid$(m_sText,m_nPos,Len(Text))=Text

m_nPos=m_nPos+Len(Text)

addanumberoftextstrings

PublicSubAppend(ParamArrayText())

DimnArgAsLong

FornArg=0ToUBound

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 解决方案 > 学习计划

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2