Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用FileTeam加快文件读取速度_C#_Performance_Stream_Filestream - Fatal编程技术网

C# 如何使用FileTeam加快文件读取速度

C# 如何使用FileTeam加快文件读取速度,c#,performance,stream,filestream,C#,Performance,Stream,Filestream,我在搜索文件内容时遇到性能问题。我正在使用FileStream类读取文件(每次搜索将涉及约10个文件,每个文件大小约为70MB)。但是,在我的搜索过程中,所有这些文件都同时被另一个进程访问和更新。因此,我不能使用Buffersize读取文件。在StreamReader中使用缓冲区大小需要3分钟,即使我使用的是regex 有没有人遇到过类似的情况,可以提供一些改进文件搜索性能的建议 代码片段 private static int BufferSize = 32768; using (Fil

我在搜索文件内容时遇到性能问题。我正在使用
FileStream
类读取文件(每次搜索将涉及约10个文件,每个文件大小约为70MB)。但是,在我的搜索过程中,所有这些文件都同时被另一个进程访问和更新。因此,我不能使用
Buffersize
读取文件。在
StreamReader
中使用缓冲区大小需要3分钟,即使我使用的是regex

有没有人遇到过类似的情况,可以提供一些改进文件搜索性能的建议

代码片段

  private static int BufferSize = 32768;
  using (FileStream fs = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        {

            using (TextReader txtReader = new StreamReader(fs, Encoding.UTF8, true, BufferSize))

            {
                System.Text.RegularExpressions.Regex patternMatching = new System.Text.RegularExpressions.Regex(@"(?=\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2})(.*?)(?=\n\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2})", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                System.Text.RegularExpressions.Regex dateStringMatch = new Regex(@"^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}");
                char[] temp = new char[1048576];
                while (txtReader.ReadBlock(temp, 0, 1048576) > 0)
                {
                    StringBuilder parseString = new StringBuilder();
                    parseString.Append(temp);
                    if (temp[1023].ToString() != Environment.NewLine)
                    {
                        parseString.Append(txtReader.ReadLine());
                        while (txtReader.Peek() > 0 && !(txtReader.Peek() >= 48 && txtReader.Peek() <= 57))
                        {
                            parseString.Append(txtReader.ReadLine());
                        }
                    }
                    if (parseString.Length > 0)
                    {
                        string[] allRecords = patternMatching.Split(parseString.ToString());
                        foreach (var item in allRecords)
                        {

                            var contentString = item.Trim();
                            if (!string.IsNullOrWhiteSpace(contentString))
                            {
                                var matches = dateStringMatch.Matches(contentString);
                                if (matches.Count > 0)
                                {

                                    var rowDatetime = DateTime.MinValue;
                                    if (DateTime.TryParse(matches[0].Value, out rowDatetime))
                                    {
                                        if (rowDatetime >= startDate && rowDatetime < endDate)
                                        {
                                            if (contentString.ToLowerInvariant().Contains(searchText))
                                            {
                                                var result = new SearchResult
                                                {
                                                    LogFileType = logFileType,
                                                    Message = string.Format(messageTemplateNew, item),
                                                    Timestamp = rowDatetime,
                                                    ComponentName = componentName,
                                                    FileName = filePath,
                                                    ServerName = serverName
                                                };
                                                searchResults.Add(result);
                                            }
                                        }
                                    }

                                }

                            }
                        }
                    }
                }
            }
        }

        return searchResults;
private static int BufferSize=32768;
使用(FileStream fs=File.Open(filePath,FileMode.Open,FileAccess.Read,FileShare.ReadWrite))
{
使用(TextReader txtReader=newstreamReader(fs,Encoding.UTF8,true,BufferSize))
{
System.Text.RegularExpressions.Regex patternMatching=new System.Text.RegularExpressions.Regex(@“(?=\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}:\d{2})(.*?(?=\n\d{4}-\d{2}-\d{2}-\d{2}:\d{2})”,System.Text.RegularExpections;
System.Text.RegularExpressions.Regex dateStringMatch=新正则表达式(@“^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}”);
字符[]临时=新字符[1048576];
而(txtReader.ReadBlock(温度,0,1048576)>0)
{
StringBuilder parseString=新建StringBuilder();
Append(temp);
if(temp[1023].ToString()!=Environment.NewLine)
{
Append(txtReader.ReadLine());
而(txtReader.Peek()>0&&!(txtReader.Peek()>=48&&txtReader.Peek()0)
{
string[]allRecords=patternMatching.Split(parseString.ToString());
foreach(所有记录中的var项)
{
var contentString=item.Trim();
如果(!string.IsNullOrWhiteSpace(contentString))
{
var matches=dateStringMatch.matches(contentString);
如果(matches.Count>0)
{
var rowDatetime=DateTime.MinValue;
if(DateTime.TryParse(匹配[0].Value,超出行DateTime))
{
如果(rowDatetime>=startDate&&rowDatetime
不久前,我不得不分析许多FileZilla服务器日志文件,每个日志文件都大于120MB。 我使用了一个简单的列表来获取每个日志文件的所有行,然后对特定行进行了出色的搜索

List<string> fileContent = File.ReadAllLines(pathToFile).ToList()
List fileContent=File.ReadAllLines(pathToFile.ToList())

但在您的情况下,我认为性能不好的主要原因不是读取文件。请尝试秒表循环的某些部分,以检查花费的时间最多。如果在像您这样的循环中多次使用Regex和TryParse,则可能会非常耗时。

您是否看过您说的多个文件被多个进程访问和更新esses,你是如何解决你的并发问题的?嗨,Yacoub,我目前的搜索并不关心文件中将来的任何更新。我所需要的是打开该文件并用一个字符串搜索它,目前可用的字符串。这是一个可怕的嵌套
if
s--计划结合一些条件吗?实际上是我的日志文件e在整个文件中的格式不同。我需要将文件从一个匹配模式读取到另一个匹配模式。匹配专利不总是在同一行中。匹配模式字符串中有换行符。因此我需要使用正则表达式来确定匹配条件。可能我需要找出优化正则表达式的方法,以提高我喜欢这场演出。