write file need to optimised for heavy traffic

后端 未结 3 757
一个人的身影
一个人的身影 2021-01-23 14:42

i am very new to C#, and this is my first question, please be gentle on me

I am trying to write a application to capture some tick data from the data provider, below is

3条回答
  •  佛祖请我去吃肉
    2021-01-23 15:25

    I would use String.Format:

    using (StreamWriter writer = new StreamWriter(@"c:\log222.txt", true))
            {
                writer.AutoFlush = true;
                writer.Write(String.Format("{0},{1},{2},{3},{4},", myoutput.time.ToString(timeFmt), 
                    myoutput.product, myoutput.type, myoutput.price, myoutput.volume);
            }
    

    If you use @ before string you don't have to use double \.

    This is much faster - you write only once to the file instead of 5 times. Additionally you don't use + operator with strings which is not the fastest operation ;)

    Also - if this is multithreading application - you should consider using some lock. It would prevent application from trying to write to the file from eg. 2 threads at one time.

提交回复
热议问题