0

I'm writing trading software and I need every single microsecond in speed. What can I do? I was thinking to use ngen but wikipedia tells that JIT might be even better. What can I tune? May I force it to use Xeon E5 instructions somehow? Will changing priority in Windows to highest help and if so how to run program always with highest priority? May I add this program to "trusted" so .NET will not check security etc.

I.e. I want complete list of configurations in hardware/software/.net and actions (like running ngen) which can affect and help run program faster.

Oleg Vazhnev
  • 23,239
  • 54
  • 171
  • 305
  • 3
    You're writing trading software in C# and you don't have experience profiling and optimizing? I hope you have a great algorithm. – Larry OBrien Jun 19 '12 at 17:55
  • What's the software doing? What are your requirements - latency, throughput, something else? This question is so broad as to be unanswerable with anything other than a laundry list of general performance suggestions, and I'm sure we already have that. – MNGwinn Jun 19 '12 at 17:55
  • Do you want to optimize **all** your program? Every single line? Why don't you rewrite in assembly? LOL Maybe you'll need to rewrite some parts in another language but first find your bottle-neck... – Adriano Repetti Jun 19 '12 at 17:56
  • 3
    You kids today with your micro-optimizations. In my day, [we wrote trading software in Visual Basic 3](http://stackoverflow.com/a/1538464/14606), uphill both ways. – MusiGenesis Jun 19 '12 at 17:59
  • "I want complete list of configurations in hardware/software/.net and actions (like running ngen) which can affect and help run program faster" is extremely broad. – RQDQ Jun 19 '12 at 20:53
  • @Adriano i don't want to rewrite anything. the question is how to tune `existent` program tuning `system`. – Oleg Vazhnev Jun 20 '12 at 05:32

3 Answers3

9

The time comes stop guessing and find where the problem(s) actually are. Use EQATEC free preformance analyzer to figure out your bottlenecks and fix them.

NGEN is usefull to boost the startup time of the application , but it's definitely not a golden key for that problem. It's most probabble that you will fix it profiling your app.

What about runtime optimization:

  • checkout DB accesses (if any), optimize your queries and minimize the data retrived to the amount you really need

  • look on disk access operations

  • look on CPU consuption. After profiling yoi can use Process Explorer to check CPU and Memory consuption from your application behavioural point of view

  • after profiling identify unnecessary or heavy iteration you made (if any), and make use of dictionary (just example) for O(1) access

... and more...

Like a literature for reading on performance can suggest definitely the monster blog of one of the greatest performance specialists in IT industry: Rico Mariani's Performance Tidbits

Hope this helps.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Tigran
  • 61,654
  • 8
  • 86
  • 123
0
  1. Not windows, pick a more real-time focused OS, as speed has several components, consistent speed or best possible?

  2. Tuning code is nice and all, but don't always go for that. Try code reviews, correcting large logical errors or extra work is much more beneficial than low level tuning.

  3. Understand that every microsecond counts, but your CPU and logic are the fastest part, network, disk, paging, etc are more than likely the real enemies. So make sure you are focusing on the right thing. You must stop all I/O which is not needed.

  4. Certain drivers, network cards, etc are faster than others.

  5. Review your use of libraries and make sure you understand their performance aspects, so much run time is within libraries etc.

Sure there are more .NET centric answers and specific ones, but I tried answering the question behind the question.

Ted Johnson
  • 4,315
  • 3
  • 29
  • 31
  • what realtime os can use suggest that supports .net applications (including wcf/wpf)? agree about other points. the question was about configuring "system" not changing my "program". – Oleg Vazhnev Jun 20 '12 at 05:26
0

Eliminate GC!

Another "trick" that people use is in trading systems is to write C# code that does not garbage collect. Obviously it is impossible to remove garbage collection entirely, but what you can do is minimize or eliminate GC once your program is running. You do all the work to setup your app, initialize your components, etc in an initialization phase and allow any GC here. But once you are setup and ready to run, you make sure that the code does not generate garbage and does not perform boxing/unboxing. (Have a search on SO for like "avoid GC" and you will find some useful information).

The term "trading system" can encompass a myriad of different things. Are you talking about writing an algo in .NET? Or is your algo written in something else and there is .NET framework that is hosting it? Are you just talking about the UI? Is your trading system distrubuted? Do you know how fast you program needs to be? If you are not trading a High Frequency model then what is "fast enough"? Don't just push for something that has to operate in the 1-2ms scale when your trading strategy does not need it.

And importantly, don't throw out the tried and trusted OO principles; SOLID still applies to trading systems although you may bend the rules in certain cases. Just make sure that you identify what needs to be performant and optimize that - don't think that you have to optimize everything and make sure you benchmark and measure everything so that you know what needs to be faster and by how much.

And Keep It Simple! A trading system does not have to be complex.

RobertMS
  • 1,153
  • 11
  • 17