问题描述
我做一个导入txt数据的程序txt里面是电话号码137822446xx139374156xx137823757xx138374892xx138374009xx159363851xx137823820xx138498657xx137823007xx135694613xx139498386xx138374348xx136298800xx139498120xx139374674xx我用遍历循环来排除时数据量小的时候没有问题当数据量大的时候运行就非常慢现在有一个txt文件有10W个电话号码我的程序就运行不了了!求一个速度快一点的排除方法!string[]arrA=File.ReadAllLines(this.tbaddress.Text.Trim());this.tbaddress.Text.Trim()文件路径for(inti=0;i<arrA.LongLength;i++){addphone1(smst,arrA[i]);}b=smst.iflistbox();
解决方案
解决方案二:
丢到数据库一个distinct
解决方案三:
通过代码优化性能是一方面,另外这样的功能,启用线程,再加进度条吧。
解决方案四:
引用楼主的回复:
addphone1(smst,arrA[i]);
读取到一个Dictionary<K,T>对象里。
解决方案五:
protectedvoidAddPhoneNoToTXT(){StreamWritersw=File.AppendText(Server.MapPath("PhoneNo.txt"));for(inti=0;i<100000;i++){sw.WriteLine("18773168000");}sw.Flush();sw.Close();}protectedvoidButton1_Click(objectsender,EventArgse){List<string>phoneNoList=newList<string>();StreamReadersr=newStreamReader(Server.MapPath("PhoneNo.txt"));while(sr.Peek()>0){phoneNoList.Add(sr.ReadLine());}sr.Close();Response.Write("合并前记录个数:"+phoneNoList.Count+"<br/>");DateTimetimeA=DateTime.Now;phoneNoList=phoneNoList.Distinct().ToList();DateTimetimeB=DateTime.Now;Response.Write("合并后记录个数:"+phoneNoList.Count+"<br/><br/>");Response.Write("合并时间:"+(timeB-timeA).ToString());}
解决方案六:
我的意思是这个txt文本中有相同的号码现在我要排除相同的号码!
解决方案七:
phoneNoList=phoneNoList.Distinct().ToList();//这句话就是去掉重复的protectedvoidButton1_Click(objectsender,EventArgse){List<string>phoneNoList=newList<string>();StreamReadersr=newStreamReader(Server.MapPath("PhoneNo.txt"));while(sr.Peek()>0){phoneNoList.Add(sr.ReadLine());}sr.Close();Response.Write("合并前记录个数:"+phoneNoList.Count+"<br/>");DateTimetimeA=DateTime.Now;phoneNoList=phoneNoList.Distinct().ToList();DateTimetimeB=DateTime.Now;Response.Write("合并后记录个数:"+phoneNoList.Count+"<br/><br/>");Response.Write("合并时间:"+(timeB-timeA).ToString());}
你只要用这个函数就好了