C# 用c语言从流中确定序列的最快方法#

C# 用c语言从流中确定序列的最快方法#,c#,performance,stream,compare,readline,C#,Performance,Stream,Compare,Readline,我需要从流中读取字节(从字符串转换为字节数组,然后发送到流),并在遇到特定序列时立即停止读取,在我的情况下,如果转换为字符串(ASCII),则为[13,10,13,10]或“\r\n\r\n” 目前,我有两个版本的相同流程: 1) 每次从流中读取一个字节,并检查读取序列的最后4个字节是否等于[13,10,13,10](请注意,我不能每4个字节读取和检查一次,例如,序列可能有7个字节长,因此它将读取前4个字节,然后卡住,因为4个字节中只有3个可用): NetworkStream streamBro

我需要从流中读取字节(从字符串转换为字节数组,然后发送到流),并在遇到特定序列时立即停止读取,在我的情况下,如果转换为字符串(ASCII),则为[13,10,13,10]或“\r\n\r\n”

目前,我有两个版本的相同流程:
1) 每次从流中读取一个字节,并检查读取序列的最后4个字节是否等于[13,10,13,10](请注意,我不能每4个字节读取和检查一次,例如,序列可能有7个字节长,因此它将读取前4个字节,然后卡住,因为4个字节中只有3个可用)

NetworkStream streamBrowser = tcpclientBrowser.GetStream();
byte[] data;
using (MemoryStream ms = new MemoryStream())
{
    byte[] check = new byte[4] { 13, 10, 13, 10 };
    byte[] buff = new byte[1];
    do
    {
        streamBrowser.Read(buff, 0, 1);
        ms.Write(buff, 0, 1);
        data = ms.ToArray();
    } while (!data.Skip(data.Length - 4).SequenceEqual(check));
}
2) 使用StreamReader.ReadLine读取到“\r\n”,然后再次读取以查看返回的行是否为空,然后添加到第一个返回的字符串“\r\n”,这样我将得到以“\r\n\r\n”结尾的字符串


我的问题是-在性能方面,哪种方法更可取(如果有的话,可能是两者都太慢了,并且有更好的方法我真的想知道)?

我喝醉了,但也许这会有帮助:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace ConsoleApp5
{
    class Program
    {
        static void Main (string[] args)
        {
            var ms = new MemoryStream (new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });

            var sequence = new byte[] { 6, 7, 8 };

            var buffer = new byte[1];
            var queue = new Queue<byte> ();
            int position = 0;

            while (true)
            {
                int count = ms.Read (buffer, 0, 1);

                if (count == 0) return;

                queue.Enqueue (buffer[0]);
                position++;

                if (IsSequenceFound (queue, sequence))
                {
                    Console.WriteLine ("Found sequence at position: " + (position - queue.Count));
                    return;
                }

                if (queue.Count == sequence.Length) queue.Dequeue ();
            }
        }

        static bool IsSequenceFound (Queue<byte> queue, byte[] sequence)
        {
            return queue.SequenceEqual (sequence);
            // normal for (int i ...) can be faster
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
名称空间控制台EAPP5
{
班级计划
{
静态void Main(字符串[]参数)
{
var ms=新内存流(新字节[]{1,2,3,4,5,6,7,8,9});
var序列=新字节[]{6,7,8};
var buffer=新字节[1];
var queue=新队列();
int位置=0;
while(true)
{
int count=毫秒读取(缓冲区,0,1);
如果(计数=0)返回;
queue.Enqueue(缓冲区[0]);
位置++;
if(IsSequenceFound(队列、序列))
{
Console.WriteLine(“在位置:+(位置-queue.Count)找到序列”);
返回;
}
if(queue.Count==sequence.Length)queue.Dequeue();
}
}
静态布尔IsSequenceFound(队列,字节[]序列)
{
返回queue.SequenceEqual(序列);
//正常(int i…)可以更快
}
}
}

您必须使用从字符串转换的字节数组吗?我的意思是,你不能在进行转换之前操纵第一个接收到的字符串吗?你打算用剩下的3个字节做什么?此外,尝试对代码进行预优化是个坏主意,您可能会惊喜地发现您编写的代码足够快:)取决于您所说的“停止阅读”的意思,因为StreamReader.ReadLine可能会从底层流中消耗比换行前更多的字节。我知道您正在使用行尾字符终止消息,但许多有线协议使用的一种真正有效的方式是消息帧。告诉服务器或客户端消息有多大,并在字节计数到达时停止。
在性能方面,哪种方法更可取