创建从列表/ SortedLists C#CSV字符串有效的方法? [英] Efficient Method for Creating CSV String from Lists/SortedLists C#?

查看:210
本文介绍了创建从列表/ SortedLists C#CSV字符串有效的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实现异步SOAP的应用程序。每50-100ms我将接收被转换成排序列表<数据;双层,双> 对象。我也有一个预定义的的IList<双重方式> 其中包含了所有可能的密钥排序列表

I have an application which implements Asynchronous SOAP. Every 50-100ms I will receive data which is converted into a SortedList<double,double> object. I also have a predefined IList<double> which contains all the possible Keys in that SortedList.

我需要通过的IList 进行迭代,并检查排序列表包含键。如果是这样,我写的值到CSV串;如果没有,我写0.0到CSV字符串

I need to iterate through the IList and check if the SortedList contains that key. If it does, I write that value to the csv string; if not, I write 0.0 to the csv string.

注意: IList中有400项。该排序列表通常会比400小很多,大约100顶多

Note: The IList has 400 keys. The SortedList will generally be much smaller than 400, around 100 at most.

        string MyText = timestamp.ToString("HH:mm:ss");
        for (int i = 0; i < AllKeys.Count; i++)
        {
            double info;
            if (MySortedList.TryGetValue(AllKeys[i], out info))
            {
                MyText += "," + info;
            }
            else
            {
                MyText += ",0.0";
            }
        }
        MyText += "\n";

        File.AppendAllText(filePath, MyText);



我目前使用上面的代码写入到我的文件之前创建的CSV字符串。然而,我发现这个代码是落后我的申请。

I currently am using the above code to create the csv string before writing it to my file. However, I am finding that this code is lagging my application.

我需要帮助提高效率,使存储输入数据需要50毫秒以下。一些额外的东西:

I need help improving the efficiency so that storing the incoming data takes below 50ms. Some additional things:


  • 我没有写入到csv文件,我只需要快速存储数据。 (我可以从一个序列化的文件后转换到我的CSV文件)

  • 我已经使用LINQ考虑,但我不熟悉的查询,不知道如何更有效它将是

编辑:我以制作StreamWriter对象的康拉德的建议,解决了我的性能问题。我只是创建了一个静态的StreamWriter对象和关闭时,通信终止的StreamWriter之前我写的所有文字吧。

I have solved my performance issue by using Conrad's suggestion of making a StreamWriter object. I simply created a static StreamWriter object and write all my text to it before closing the StreamWriter when communication is terminated.

推荐答案

下面是一些想法。

1)使用的的StreamWriter 来写,而不是文件。这将是比两个步骤写入存储器,然后以一个文件的要快。

1) Use a StreamWriter to write instead of File. This will be faster than the two step of writing to memory and then to a file.

2)如果它的所有可能的并行工作。例如,如果你可以在你一个线程处理消息,而另一个线程来写邮件。

2) If it all possible parallelize the work. For example if you can you one thread for processing the message and another thread to write the message.

3)我不认为LINQ的目的是为了提高性能但使数据更易于

3) I don't think the intent of LINQ is to improve performance, but make manipulations of data easier

这篇关于创建从列表/ SortedLists C#CSV字符串有效的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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