使用C#在大文本文件中使用特殊caractere过滤所有电子邮件的速度更快 [英] Very faster way to filter all emails end with a special caractere in a big text file using C#

查看:77
本文介绍了使用C#在大文本文件中使用特殊caractere过滤所有电子邮件的速度更快的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



i希望以更快的方式过滤所有使用c#


我尝试过:



var lines = File.ReadAllLines(file路径);



foreach(行中的行数)

{

if(line.EndsWith(myWord) ))

{

outputEmails.Text + = line + Environment.NewLine;

}

}

Hello ,

i want a very faster way to filter all emails that end with a special caracter in a big text file (more than 200MB) using c#

What I have tried:

var lines = File.ReadAllLines(file path);

foreach(var line in lines)
{
if (line.EndsWith(myWord))
{
outputEmails.Text += line + Environment.NewLine;
}
}

推荐答案

好吧,你可以通过用StringBuilder替换字符串连接来使它更快一点。为什么?因为.NET中的字符串是不可变的。创建后,您无法更改它们。你在连接中所做的就是每次向它添加一些东西时创建一个新的字符串对象。



你也可以通过更换来获得非常小的速度提升带有StreamReader的.ReadAllLines并一次一行地处理文件。你还可以节省200MB的内存和一些页面交换。



如果你想在每一行的末尾找一个字符,你可能会能够通过将每一行(将是一个字符串)作为一个char数组来处理另一个提升,并特别查看该字符的数组中的最后一个索引。这将避免.EndsWith方法的一些开销。你正在编写专门用于查看最后一个字符的内容,而不是使用查找整个字符串的更一般的用例。



其他制作方法它更快地涉及花时间预处理文本文件并创建索引。这将花费您现在用于构建索引的时间量。然后通过使用索引获得所需的行很容易且更快。但是,我不认为这就是你所说的。
Well, you can make it a tiny bit faster by replacing that string concatenation with a StringBuilder. Why? Because strings in .NET are immutable. Once created you cannot change them. What you're doing with that concatenation is creating a new string object every time you append something to it.

You could also probably pickup a very minor speed improvement by replacing the .ReadAllLines with a StreamReader and processing the file line by line one at a time. You'll also save yourself 200MB of memory and possibly some page swapping.

If you're looking for a single character at the end of each line, you might be able to eek out another boost by treating each line, which will be a string, as an array of char and specifically look at the last index in the array for that character. This will avoid some of the overhead of the .EndsWith method. You're writing something that specializes in looking at the last character instead of using the more general use case that looks for an entire string of characters.

Other methods to make it faster involve taking the time to pre-process the text file and create an index. This will take the amount of time your taking now to build the index. Then it's easy enough and much faster to get the lines that you need by using the index. But, I don't think that's what you're talking about.


这篇关于使用C#在大文本文件中使用特殊caractere过滤所有电子邮件的速度更快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆