如何编写高效简洁的c语言(how to write efficient and concise c language)

如何编写高效简洁的c语言(how to write efficient and concise c language)

ID:11592172

大小:20.81 KB

页数:10页

时间:2018-07-12

如何编写高效简洁的c语言(how to write efficient and concise c language)_第1页
如何编写高效简洁的c语言(how to write efficient and concise c language)_第2页
如何编写高效简洁的c语言(how to write efficient and concise c language)_第3页
如何编写高效简洁的c语言(how to write efficient and concise c language)_第4页
如何编写高效简洁的c语言(how to write efficient and concise c language)_第5页
资源描述:

《如何编写高效简洁的c语言(how to write efficient and concise c language)》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

1、如何编写高效简洁的c语言(HowtowriteefficientandconciseClanguage)Introduction:ThegoalofmanysoftwareengineersistowriteefficientandconciseClanguagecode.Inthispaper,theworkofsomeexperienceandexperiencetodorelatedelaboration,nottheplace,pleaseadvise.Thefirsttrick:takespacefortimeThebiggestcontradi

2、ctioninacomputerprogramisacontradiction,spaceandtimethen,startingfromtheangleofreversethinkingtoconsidertheefficiencyoftheprogram,wehavetosolvetheproblemofthefirststrokes--aspacefortime.Forexample,theassignmentofstrings.MethodA,theusualway:#defineLEN32Charstring1[LEN];Memset(strin

3、g1,0,LEN);Strcpy(string1,"Thisisaexample!!");MethodB:Constcharstring2[LEN]="Thisisaexample"!";Char*cp;CP=string2;Itcanbeoperateddirectlywithpointerwhenusing.)Fromtheaboveexample,wecanseethattheefficiencyofAandBisnotcomparable.Inthesamestoragespace,Bcanoperatedirectlywithpointers,a

4、ndAneedstocalltwocharacterfunctionstocomplete.TheweaknessofBisflexibility,notA.Whenyouneedtochangeastringcontentfrequently,Ahasbetterflexibility;ifusingtheBmethod,youneedtodepositmanystrings,althoughoccupyalargeamountofmemory,buttheprogramexecutionefficiency.Ifthesystemhashighreal

5、-timerequirementsandmemory,thenIrecommendyouusethistrick.Thetrickofthistrickistousemacrosinsteadoffunctions.Examplesareasfollows:MethodC:#definebwMCDR2_ADDRESS4#definebsMCDR2_ADDRESS17IntBIT_MASK(int__bf){Return((1U(BW##__bf))-1(BS),__bf##);}VoidSET_BITS(int__dst,int__bf,int__val)

6、{__dst=((__dst)&(BIT_MASK~(__bf))

7、)(((__val)(BS<##&(__bf))BIT_MASK(__bf))))}SET_BITS(MCDR2,MCDR2_ADDRESS,RegisterNumber);MethodD:#definebwMCDR2_ADDRESS4#definebsMCDR2_ADDRESS17#definebmMCDR2_ADDRESSBIT_MASK(MCDR2_ADDRESS)#defineBIT_MASK(__bf)((("1U(BW__bf##))-1(BS),__bf##))#define

8、SET_BITS(__dst,__bf,__val)((__dst)=((__dst)&(BIT_MASK~(__bf))

9、)(((__val)(BS<##&(__bf))BIT_MASK(__bf))))SET_BITS(MCDR2,MCDR2_ADDRESS,RegisterNumber);Thedifferencebetweenfunctionandmacrofunctionisthatthemacrofunctiontakesupalotofspace,andthefunctiontakesuptime.Youneedtoknowisthatthe

10、functioncallistousethesystemstack

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。