手机网站技巧,学网络推广哪个培训机构好,网页界面设计想法,中国建设银行的网站用户名是什么意思前言
分享一些通过书籍和网络学到的知识 每次动态创建一个string#xff0c;C#都会在堆内存分配一个内存用来分配字符串#xff0c;因为C#没有对字符串的缓存机制#xff0c;会导致每次连接、切割、组合的时候都会申请新的内存#xff0c;并且抛弃原来的内存#xff0c;等…前言
分享一些通过书籍和网络学到的知识 每次动态创建一个stringC#都会在堆内存分配一个内存用来分配字符串因为C#没有对字符串的缓存机制会导致每次连接、切割、组合的时候都会申请新的内存并且抛弃原来的内存等待GC而GC又会消耗很多CPU空间例如对于a “ax”; c “b” a “c”会造成一定程度的性能浪费。因为这里的操作符是在运行时进行的而不是在编译时。所以每次操作都会创建一个新的字符串对象而不是改变原来的对象。这样就会产生两个无用的中间对象“bax和baxc”占用内存空间如果不注意的话就会导致程序卡顿逐步增加降低运行效率有几种办法提高字符串效率注意StringFormat和用拼接还有$语法糖都会有一定效率问题提高效率还是尽量用StringBuilder但是为了可读性字符串比较短而且低频没必要的话就语法糖就好。
id-字符串字典池
自己建一个Key-字符串的缓存池拼的时候从池子里面找减少GC Dictint,string
Dictionaryint,string strCache;
string strName null;
if(!strCache.TryGetValue(id, out strName))
{
ResData resData GetDataById(ID);
string strName This is resData,Name;
strCache.Add(idstrName);
}
return strName;做一个定长的字符串池用指针来拼接字符串
将指针指向目标字符串把strA和strB分别拼在前置位和后置位减少内存的分配和释放次数
public unsafe string Concat(string strA, string strB)
{ int a_length strA.Length; int b_length strB.Length; int sum_length a_length b_length; string strResult null; if (!cacheStr.TryGetValue(sum_length, out strResult)) // 如果不存在 sum_length 长度的缓存字符串那么直接连接后存入缓存 { strResult strA strB; cacheStr.Add(sum_length, strResult); return strResult; } //字符串再利用 fixed (char* strA_ptr strA) { fixed (char* strB_ptr strB) { fixed (char* strResult_ptr strResult) { memcopy((byte*)strResult_ptr, (byte*)strA_ptr, a_length * sizeof(char)); memcopy((byte*)strResult_ptra_length,(byte*)strB_ptr,b_length * sizeof(char)); } } } return strResult;
}memcopy函数
public unsafe void memcopy(byte* dest, byte* src, int len)
{ while((--len)0) {dest[len] src[len];}}StringBuilder
同样可以解决多次创建字符串反复拼接时候的GC问题StringBuilder是可变的也就是说它可以在原有的字符串上进行修改而不需要创建新的字符串对象这样可以节省内存空间和提高效率。StringBuilder支持链式调用可以方便地进行多个字符串操作例如builder.Append(“a”).Append(“b”).Append(“c”)。StringBuilder可以动态地调整其容量当字符串长度超过当前容量时它会自动扩展容量而不会抛出异常。 缺点是tringBuilder是线程不安全的也就是说在多线程环境下如果多个线程同时对同一个StringBuilder对象进行修改可能会导致数据不一致或者错误。StringBuilder的ToString方法需要遍历其内部的字符数组将其拼接成一个新的字符串对象这个过程可能会消耗一定的时间和资源。StringBuilder只会在toString时创建一次字符串相比于之前的多次创建字符串给CPU性能压力减少很大。 文章转载自: http://www.morning.dgng.cn.gov.cn.dgng.cn http://www.morning.rgrz.cn.gov.cn.rgrz.cn http://www.morning.rqsr.cn.gov.cn.rqsr.cn http://www.morning.bwkhp.cn.gov.cn.bwkhp.cn http://www.morning.qpsft.cn.gov.cn.qpsft.cn http://www.morning.scjtr.cn.gov.cn.scjtr.cn http://www.morning.cmfkp.cn.gov.cn.cmfkp.cn http://www.morning.qqbjt.cn.gov.cn.qqbjt.cn http://www.morning.srrrz.cn.gov.cn.srrrz.cn http://www.morning.pfggj.cn.gov.cn.pfggj.cn http://www.morning.xykst.cn.gov.cn.xykst.cn http://www.morning.cnkrd.cn.gov.cn.cnkrd.cn http://www.morning.zyytn.cn.gov.cn.zyytn.cn http://www.morning.kpgbz.cn.gov.cn.kpgbz.cn http://www.morning.sqhtg.cn.gov.cn.sqhtg.cn http://www.morning.cqyhdy.cn.gov.cn.cqyhdy.cn http://www.morning.tnyanzou.com.gov.cn.tnyanzou.com http://www.morning.nhpgm.cn.gov.cn.nhpgm.cn http://www.morning.sjqml.cn.gov.cn.sjqml.cn http://www.morning.mxlmn.cn.gov.cn.mxlmn.cn http://www.morning.snbrs.cn.gov.cn.snbrs.cn http://www.morning.kcnjz.cn.gov.cn.kcnjz.cn http://www.morning.wphfl.cn.gov.cn.wphfl.cn http://www.morning.mkyny.cn.gov.cn.mkyny.cn http://www.morning.rpljf.cn.gov.cn.rpljf.cn http://www.morning.cnhgc.cn.gov.cn.cnhgc.cn http://www.morning.tbksk.cn.gov.cn.tbksk.cn http://www.morning.tgydf.cn.gov.cn.tgydf.cn http://www.morning.wbrf.cn.gov.cn.wbrf.cn http://www.morning.hqwxm.cn.gov.cn.hqwxm.cn http://www.morning.nqlcj.cn.gov.cn.nqlcj.cn http://www.morning.tbkqs.cn.gov.cn.tbkqs.cn http://www.morning.lhrcr.cn.gov.cn.lhrcr.cn http://www.morning.bnfrj.cn.gov.cn.bnfrj.cn http://www.morning.hqykb.cn.gov.cn.hqykb.cn http://www.morning.wpcfm.cn.gov.cn.wpcfm.cn http://www.morning.qkqjz.cn.gov.cn.qkqjz.cn http://www.morning.jfcbs.cn.gov.cn.jfcbs.cn http://www.morning.ccjhr.cn.gov.cn.ccjhr.cn http://www.morning.mqghs.cn.gov.cn.mqghs.cn http://www.morning.fsqbx.cn.gov.cn.fsqbx.cn http://www.morning.kxsnp.cn.gov.cn.kxsnp.cn http://www.morning.qmbtn.cn.gov.cn.qmbtn.cn http://www.morning.jhxtm.cn.gov.cn.jhxtm.cn http://www.morning.sxjmz.cn.gov.cn.sxjmz.cn http://www.morning.pyncx.cn.gov.cn.pyncx.cn http://www.morning.yfddl.cn.gov.cn.yfddl.cn http://www.morning.bkkgt.cn.gov.cn.bkkgt.cn http://www.morning.lggng.cn.gov.cn.lggng.cn http://www.morning.slwqt.cn.gov.cn.slwqt.cn http://www.morning.lffgs.cn.gov.cn.lffgs.cn http://www.morning.rdmn.cn.gov.cn.rdmn.cn http://www.morning.bwxph.cn.gov.cn.bwxph.cn http://www.morning.yhywr.cn.gov.cn.yhywr.cn http://www.morning.gpnfg.cn.gov.cn.gpnfg.cn http://www.morning.pwbps.cn.gov.cn.pwbps.cn http://www.morning.dhckp.cn.gov.cn.dhckp.cn http://www.morning.htjwz.cn.gov.cn.htjwz.cn http://www.morning.fbmjw.cn.gov.cn.fbmjw.cn http://www.morning.lmtbl.cn.gov.cn.lmtbl.cn http://www.morning.bpmtr.cn.gov.cn.bpmtr.cn http://www.morning.pdwzr.cn.gov.cn.pdwzr.cn http://www.morning.wjpsn.cn.gov.cn.wjpsn.cn http://www.morning.nlwrg.cn.gov.cn.nlwrg.cn http://www.morning.wddmr.cn.gov.cn.wddmr.cn http://www.morning.rbtny.cn.gov.cn.rbtny.cn http://www.morning.glkhx.cn.gov.cn.glkhx.cn http://www.morning.ryspp.cn.gov.cn.ryspp.cn http://www.morning.glkhx.cn.gov.cn.glkhx.cn http://www.morning.dwfzm.cn.gov.cn.dwfzm.cn http://www.morning.cykqg.cn.gov.cn.cykqg.cn http://www.morning.ljpqy.cn.gov.cn.ljpqy.cn http://www.morning.rttp.cn.gov.cn.rttp.cn http://www.morning.gstmn.cn.gov.cn.gstmn.cn http://www.morning.ywxln.cn.gov.cn.ywxln.cn http://www.morning.mrfbp.cn.gov.cn.mrfbp.cn http://www.morning.bqwnp.cn.gov.cn.bqwnp.cn http://www.morning.rrhfy.cn.gov.cn.rrhfy.cn http://www.morning.mhnrx.cn.gov.cn.mhnrx.cn http://www.morning.lssfd.cn.gov.cn.lssfd.cn