c# - Write file need to optimised for heavy traffic part 2 -


for interest see come can refer part 1, not necessary.

write file need optimised heavy traffic

below snippet of code have written capture financial tick data broker api. code run without error. need optimize code, because in peak hours zf_tickevent method call more 10000 times second. use memorystream hold data until reaches size, output text file.

the broker api single threaded.

void zf_tickevent(object sender, zenfire.tickeventargs e) {      outputstring = string.format("{0},{1},{2},{3},{4}\r\n",                         e.timestamp.tostring(timefmt),                         e.product.tostring(),                         enum.getname(typeof(zenfire.ticktype), e.type),                         e.price,                         e.volume);      fillbuffer(outputstring);  }  public class memorystreamclass {     public static memorystream ms = new memorystream(); }  void fillbuffer(string outputstring) {      byte[] outputbyte = encoding.ascii.getbytes(outputstring);      memorystreamclass.ms.write(outputbyte, 0, outputbyte.length);      if (memorystreamclass.ms.length > 8192)     {         emptybuffer(memorystreamclass.ms);         memorystreamclass.ms.setlength(0);         memorystreamclass.ms.position = 0;     } }  void emptybuffer(memorystream ms) {     filestream outstream = new filestream("c:\\test.txt", filemode.append);      ms.writeto(outstream);     outstream.flush();     outstream.close(); } 

question:

  1. any suggestion make faster? try vary buffer length in terms of code structure, (almost) fastest?

  2. when memorystream filled , emptying file, happen new data coming in? need implement second buffer hold data while emptying first buffer? or c# smart enough figure out?

thanks advice

the fastest way have 1 (or more) threads put byte[]'s blockingcollection , have 1 thread take items out fast possibly can , write them file. way producers , file-writing consumer totally decoupled. able sustain high load doing that.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -