C#低效流式传输到硬盘 [英] C# inefficient streaming to the hard disk

查看:138
本文介绍了C#低效流式传输到硬盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个程序,该程序接收每个数据包大小为1460字节的UDP数据包,并将这些字节连续流式传输到PC的硬盘驱动器上.目前,我的程序只能在数据丢失发生之前接收和流式传输84个数据包.我需要我的程序是实时的,具有20 kHz的快速采样率.我需要能够接收更多的数据包而不会丢失.

我的代码连接到客户端,并等待数据输入.当数据输入时,程序将接收数据,将数据流传输到硬盘驱动器,然后等待更多数据.我认为我的流传输速度不够快,因此无法接收下一个发送过来的数据包.

这是一些代码:

I am currently working on a program that receives UDP packets of size 1460 bytes per packet and continuously streams the bytes to the hard drive of my PC. Currently, my program can only receive and stream 84 packets before data loss occurs. I need my program to be realtime with a fast sampling rate of 20 kHz. I need to be able to receive much more packets of data without loss.

My code connects to the client and waits for data to come in. When data comes in, the program receives the data, streams to the hard drive, then waits for more data. I think that I am not streaming fast enough, so I fail to receive the next packet of data that gets sent in.

Here is some code:

if (firsttimeflag == false) // Streaming each value to the hard disk 
   {
    using (BinaryWriter b = new BinaryWriter(File.Open(@"F:\file.bin", FileMode.Create)))
       {
           //Use foreach and write all values.
           foreach (int i in datain)
           {
             b.Write(i); // Writes a four byte signed integer.
           }
       }
             firsttimeflag = true;
     }
 else
     {
      using (BinaryWriter b = new BinaryWriter(File.Open(@"F:\file.bin", FileMode.Append)))
          {
            foreach (int i in datain)
              {
                b.Write(i);
              }
          }
      }



其中"datain"是1460个字节的数组,该数组已预先转换为字符串.

任何帮助,将不胜感激.我的代码效率低下在哪里?

谢谢,
GabeA



Where "datain" is the 1460 byte array which is converted to a string beforehand.

Any help would be appreciated. Where is the inefficiency of my code?

Thanks,
GabeA

推荐答案

为什么用这种方式弄乱数据?您从网络接收到字节流,因此您所要做的就是将该流(无论接收到的长度)写入磁盘.不要尝试转换为字符串(有什么作用?),也不要将四个字节写为int.
Why are you messing about with the data in this way? You receive a stream of bytes from the network so all you need to do is write that stream (whatever length you receive) to the disk. Don''t try converting to a string (what does that achieve?) and don''t write four bytes as an int.


我对速度有帮助的几件事前...

打开文件并使其保持打开状态,并在数据进入时对其进行写入,然后在程序退出时关闭该文件.每个数据包的打开和关闭文件只是开销,可以轻松删除.

为什么要自己将每个"int"写入文件,为什么不只发送整个"datain"缓冲区以写入磁盘呢?它消除了循环,这些循环只是更多无用的开销.

另外,您可能想检查未缓冲的文件写入,因为由C#和Windows完成的缓冲将意味着您的程序将暂停",因为Windows/C#意识到缓冲区已满,应将其写入磁盘...您将最终如果您不幸,使用Java式GC的Java会暂停缓冲.
I few things which should help on the speed front...

Open the file and keep it open, and just write data to it as it comes in, and then close the file when the program exits. Open and closing files for each packet is just overhead which can easily removed.

Why are you writing each ''int'' by itself to the file, why not just send the whole ''datain'' buffer to be written to disk? It removes the loop(s) which are just more useless overhead.

Also you may want to check out unbuffered file writes, as buffering done by C# and Windows will mean your program will ''pause'' as Windows/C# realises the buffer is full and should be written to disk... You shall end up with Java-ish GC pauses with buffering if your are unlucky.


这篇关于C#低效流式传输到硬盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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