views:

220

answers:

3

I'm interested in seeing what considerations experienced developers make when developing high performance multithreaded GUI's for a windows platform. I ask this question in the context of developing trading applications where GUI's are very dynamic and application latency is an issue.

What architectures have you seen or would you recommend looking at over MFC document/view to implement the observer pattern in this context. I believe document/view wouldn't be used due to performance issues.

What specific considerations need to be made to UI components/windows that are being updated in a separate thread, both in MFC and Qt? Are there any general rules that would apply to all GUI libraries?

+7  A: 

You're looking in entirely the wrong places. The "overhead" of a document/view architecture is in the nanosecond range (basically, accessing data via a pointer).

For comparison, the absolute maximum rate at which you can meaningfully update the screen is the monitor's refresh rate, which is typically 60 Hz (i.e., once every 16.67 milliseconds).

To make even that kind of refresh rate meaningful, you can't really change much in any given monitor update -- if you try to change too much, the user won't be able to follow what's going on.

As far as threading goes, by far the simplest method is to do all the actual window updating in one thread, and use the other threads for doing computations and such that generate data for the window being updated. As long as you assure that thread doesn't need to do a lot of computation and such, updating the window as fast as there's any use for is pretty easy.

Edit: As far as C++ vs. C# goes, it depends. I have no doubt at all the you can get entirely adequate display performance from either one. The real question is how much computation you're doing behind those displays. What you've mentioned has been displaying primarily pretty close to raw data (price, volume, etc.) For that, C# will probably be just fine. My guess would be that the people you've talked to are doing considerably more computation than that -- and that's the real Achilles heal of .NET (or almost anything else that runs on a VM). From what I've seen, for really heavy duty computation, C# and such aren't really very competitive.

Just for example, in another answer a while back I mentioned an application I originally wrote in C++, that another team rewrote in C#, which ran about 3 times slower. Since posting that, I was curious and talked with them a bit more about it, asking whether they couldn't improve its speed to be at least close to the same as C++ with a little extra work.

Their reply was, in essence, that they'd already done that extra work, and it was not just "a little". The C# rewrite took something like 3 1/2-4 months. Of that time, it took them less than a month to duplicate the features of the original; the entire rest of the time was spent on (trying to) make it fast enough to be usable.

I hasten to caution that 1) this is only one data point, and 2) I've no idea how close it is to anything you might do. Nonetheless, it does give some idea of the kind of slowdown you could run into when (and if) you start to do real computation rather than just passing data through from the network to the screen. At the same time, a quick look indicates that it's generally in line with the results on the Computer Language Shootout web site -- though keep in mind the results there are for Mono rather than Microsoft's implementation.

At least to me, the real question comes down to this: is your concern for performance really well-founded or not? For something like 90% of the applications around, what's important is simply that the code does what you want it to, and speed of execution matters little, as long as it doesn't get drastically slower (e.g., hundreds or thousands of times slower). If your code falls into that (large) category, C# may very well be a good choice. If you really do have a good reason to be concerned about speed of execution, then it seems to me choosing C# would be a lot more questionable.

Jerry Coffin
For this type of application, would you say theres any good reason to choose c++ over c#.net when it comes to latency? I know the metrics I've given are very vague. There does seem to be a preference in Londons financial development circles to opt for C++ for reasons of latency. Maybe it's really about not being able to justify a re-write in c#.net.
Raf
igouy
+2  A: 

Since you mentioned in a comment the issue of choosing C++ or C#, I'm going to recommend C# and especially WPF (Windows Presentation Foundation). Theoretically, a C++ application has a higher performance ceiling than a .Net application, since it doesn't have the .Net framework overhead to deal with. But it's also going to take longer to develop (probably) and be more prone to errors and memory leaks.

If you're going to be writing your own display controls, WPF (or even WinForms) is plenty fast enough to handle this kind of control load (if, like with any language/platform, it's written correctly). Moreover, there are a huge number of custom controls available that do exactly this sort of thing (displaying stock charts and whatnot) that would make the construction of this application much faster than doing everything yourself from scratch.

MusiGenesis
I'd agree with all the good reasons to go for .net and associated technologies. Why do you think City of London is still stuck on c++ for these types of apps though? I was under the impression that this was one of the few niche areas that c++ was preferred for Gui applications.
Raf
TBH I've worked for a few of the investment banks in London and everywhere seems to be using C# for the GUI with C++ for the calc. libraries (and either COM or C++/CLI to bridge). I've not seen any MFC GUI code for a long time other than a couple of ancient legacy systems that were on their way out anyway.
Phil Nash
That's good to know. It could just be that the companies inviting me for interview were the ones with the legacy systems interested in the years of MFC I did long ago. Maybe I should strike it off the CV.
Raf
@Raf: "City of London" doesn't know squat about programming, a fact that programmers working *for* City of London exploit to their advantage by using a platform/language that benefits their own job security at the expense of their employer. C++ is (usually) blazing fast, but so is everything else these days (even Java and *except* RoR). I think the cost of development time drastically outweighs everything else in software. If I were actually paying for software to be written, C++ is the last thing I would use.
MusiGenesis
OK, that was a little extreme. C++ has its place, of course; I'm just not 100% sure that this is it.
MusiGenesis
It's not. Outside of light, system-tray style apps, there is virtually zero benefit in using C++ for GUI. On the calculation side, however, that's a difference story. And it's not just raw computation speed - if you're distributing small calcs on a grid the start-up time of a VM tends to dominate!
Phil Nash
+2  A: 

I have worked on the GUI side of a trading application. Basically, anything local (i.e. non-Web) is fast enough. C++, C# or Java would all do. The main disadvantage of using C++ is that it removes the natural barrier between the calculation code and the UI. The programmers before me had been a bit sloppy, used C++, and thus the calculation code and the UI were somewhat intertwined. That made the Qt port harder.

Multithreading is mostly irrelevant to the UI. It should run on its own thread, though, and that means that only the interface to the calculation engine(s) need to be concerned with the possibility of being called on other threads.

MSalters
LIked your point about forcing a split between presentation and data/calculation layers.
Raf