8

Could anyone give me some pointers as to the best way in which to learn how to do very low latency programming? I have many programming books but I've never seen one which focused (or helped) on writing extremely fast code. Or are books not the best way forward?

Some advice from an expert would be really appreciated!

EDIT: I think I'm referring more to CPU/Memory bound.

Oleg Vazhnev
  • 23,239
  • 54
  • 171
  • 305
Tom
  • 419
  • 1
  • 5
  • 7
  • 4
    Depends on what that code is? Is the code IO bound, or cpu bound? There's no point having hand-crafted assembly if you're going to be waiting on a database and there's no point having a google-standard filesystem if all you want to do is write a text file. –  Jan 26 '11 at 22:34
  • 3
    The answer for only one language would take books and you expect to get answers for C++/Java/C# : that's gonna take libraries. So I guess you will need to concertize your question if you expect some answers. – Darin Dimitrov Jan 26 '11 at 22:34
  • 4
    Step 1: Write Program. Step 2: Measure Performance. Step 3: If Performance is garbage, optimize in hot spots. Step 4. Goto Step 2. – Billy ONeal Jan 26 '11 at 22:37
  • @Billy, you could insert an intermediary step between 3 and 4: ask a specific question on StackOverflow by presenting your particular scenario, showing the code you are working on, and asking how it could be optimized because right now you are experiencing some performance problems with it. – Darin Dimitrov Jan 26 '11 at 22:41
  • When I think low-latency, I think Erlang. Not good at number crunching, but can respond to parallel requests very quickly. Very different from high performance computer modeling (such as climate modeling). – ILMTitan Jan 26 '11 at 22:52
  • I personally found the question interesting, but as per @Darin's suggestion, you need to make it more specific. – Kyle Rosendo Jan 27 '11 at 05:07
  • 1
    Darin, if you're going to be sarcastic- you could at least be correct. Why does somebody need a specific scenario to answer a question? Perhaps they need to ask the question before getting to that particular scenario. So leave the scarcasm at the door and carry on with your C# GUI programming. – intrigued_66 May 11 '14 at 03:56

3 Answers3

9

[C++ programmer]:

Ultra-low-latency programming is hard. Much harder than people suspect when they first start down the path. There are some techniques and "tricks" you can employ. Like IO Completion ports, multi core utilization, highly optimized synchronization techniques, shared memory. The list goes on forever. (edit) It's not as simple as "code-profile-refactor-repeat" because you can write excellent code that is robust and fast, but will never be truly ultra-low latency code.

Unfortunately there is no one single resource I know of that will show you how it's done. Programmers specializing in (and good at) ultra low-latency code are among the best in the business and the most experienced. And with good reason. Because if there is a silver bullet solution to becoming a good low-latency programmer, it is simply this: you have to know a lot about everything. And that knowledge is not easy to come by. It takes years (decades?) of experience and constant study.

As far as the study itself is concerned, here's a few books I found useful or especially insightful for one reason or another:

John Dibling
  • 99,718
  • 31
  • 186
  • 324
4

My advice would be to learn how C++/Java/C# works and get a general idea of how it translates into machine level instructions. Some operations are more expensive that others. A little code snippet in a high-level language translates into a whole bunch of machine instructions (often more than you would think.)

Also, be sure to learn the various data structures, what they are good for, and their performance characteristics for various problem sizes. Choosing the correct data structure for the problem can do wonders for execution speed.

Zach Johnson
  • 23,678
  • 6
  • 69
  • 86
1

Start by learning assembly. Then if you're going to work in Java or .NET learn their interpreted bytecode. Once you've gotten that far you need to understand the language you are working in, and the compiler you're working on. Keep on that path and you'll collect bits of knowledge like how with both VC++ and GCC ternary operators (?:) result in creation of temporary values whereas an equivalent if statement will not.

Then after about 10 years you'll be able to write pretty good low-latency code.

Unfortunately there is no quick way to learn this.

Good books to learn about C++: Effective C++ More Effective C++ Exceptional C++

Beanz
  • 1,957
  • 1
  • 13
  • 14
  • 1
    +1: "Then after about 10 years you'll be able to write pretty good low-latency code." So true. – John Dibling Jan 26 '11 at 22:47
  • I learnt ARM assembly at university (and very briefly MIPS) would you recommend learning the Intel? – Tom Jan 26 '11 at 22:49
  • 1
    If you are writing code for intel PCs, then yes. That said understand that as a professional developer you should rarely if ever write assembly code. That said it will make you a better engineer to be able to understand assembly well. – Beanz Jan 26 '11 at 22:56