做一个把实时数据保存到txt文件的功能。
      比如要在5秒内向某个建好的txt文件里写入20000个数据。
         有没有什么好的办法。
           目前我用的是一个数组来存储这些数据 string [] allDateList=new string [30000];
              当积累够30000个数据的时候向txt文件写一次,如果不这样做,写的次数太频繁,效率更低。
              然后用for循环写入,三十个数据写一行.
                Filestream fsm=new FileStream("参数");                StreamWrite datefsm =new FileStream(fileName, FileMode.CreateNew, FileAccess.Write);
                for(int i=0;i<(allDateList.Length/30);i++)
                  {
                    datefsm.wiriteLine(allDateList[(i*30)+0],allDateList[(i*30)+1].................
                    ......allDateList[(i*30)+29]);
                  }这样写,效率不能满足我的需求,有没有更高效率的解决方案。坐等高人。(上面的是手写的伪代码,可能有些地方写错了,将就看一下,理解意思就行)

解决方案 »

  1.   

    不知道你的20000个数据具体是多大?
    每秒中能有50Mbytes吗?
    感觉没必要使用内存映射文件,也许读操作的时候内存映射文件要占优势一些,再写操作的时候没有明显优势。
    楼主还是注意下程序结构和数据的组织方式吧。
      

  2.   


    每秒没有50MByte的数据。数据就是很简单的十进制数据。有具体的建议吗?谢谢
      

  3.   

    建议就是重写程序
            static void Main(string[] args)
            {            string[] strData = new string[3000000];
                for (int i = 0; i < 3000000;i++ )
                {
                    strData[i] = "this is a Test string this is a Test stringthis is a Test stringthis is a Test string";
                }            Stopwatch timeWatch = new Stopwatch();
                timeWatch.Start();
                using (StreamWriter sw = new StreamWriter("CDriveDirs.txt"))
                {
                    foreach (string item in strData)
                    {
                        sw.WriteLine(item);
                    }
                }
                timeWatch.Stop();
                Console.WriteLine(timeWatch.Elapsed.TotalSeconds.ToString()+"秒");
                Console.ReadLine();        }300万个字符串在我电脑上3.5秒
      

  4.   

    C#:内存流 MemoryStream
       MemoryStream ms=new MemoryStream ();实例后使用,可以用ReadLine()方法写数据到.txt文档中。