资源描述:
《code execution efficiency issues(代码执行效率问题)》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、Codeexecutionefficiencyissues(代码执行效率问题)ReprintedanarticleonhowtooptimizeandimprovetheefficiencyoftheJAVAcode(2010-04-1814:43:06)reprintlabels:GossipTheresourcesavailable(memory,CPUtime,networkbandwidth,etc.)arelimited,andthegoalofoptimizationistoallowtheprogramtocompletetheintendedtas
2、kwithaslittleresourcesaspossible.Optimizationusuallyconsistsoftwoaspects:reducingthevolumeofcodeandimprovingtheefficiencyofthecode.Thisarticlefocusesonhowtoimprovetheefficiencyofcode.InJavaprograms,mostoftheperformanceproblemslienotintheJavalanguage,butintheprogramitself.Itisimportant
3、todevelopgoodcodinghabits,suchasusingjava.lang.Stringclassesandjava.util.Vectorclassescorrectlyandskillfully,whichcansignificantlyimproveprogramperformance.Nowlet'sanalyzetheproblemindetail.1,trytospecifytheclass'sfinalmodifier,andtheclasswiththefinalmodifierisnotderivative.IntheJavak
4、ernelAPI,therearemanyexamplesofapplyingfinal,suchasjava.lang.String.SpecifyingfinalfortheStringclasspreventspeoplefromcoveringthelength()method.Also,ifaclassisspecifiedasfinal,allmethodsofthatclassarefinal.TheJavacompilerlooksforallthefinalmethodsoftheinline(whicharerelatedtospecificc
5、ompilerimplementations).Thiswillincreaseperformancebyanaverageof50%.2,reuseobjectsasmuchaspossible.IntheuseofStringobjects,inparticular,StringBufferisusedinsteadofstringconnections.Sincethesystemnotonlytakestimetogenerateobjects,itmaytaketimetorecycleandprocessthoseobjectslater.Thus,g
6、eneratinganexcessivenumberofobjectscanhaveasignificantimpactontheperformanceofyourprogram.3,trytouselocalvariables,theargumentspassedbythecallmethod,andthetemporaryvariablescreatedinthecallarestoredinthestack(Stack),faster.Othervariables,suchasstaticvariables,instancevariables,arecrea
7、tedintheheap(Heap),whichisslower.Inaddition,dependingonthespecificcompiler/JVM,localvariablesmayalsobefurtheroptimized.Seetheuseofstackvariableswheneverpossible.4,donotrepeattheinitializationofvariablesbydefault,calltheconstructoroftheclass,Javawillbeinitializedtodeterminethevaluesofv
8、ariab