问题描述
我的交易软件是很慢,我想提高它。有两个瓶颈
My trading software is pretty slow, I want to boost it. There are two bottle-necks.
第一瓶颈:
当新的数据串被接收(新的报价,交易等)的所有战略需要尽快更新。他们需要重新计算其状态/订单等。当新的数据串是准备读 AllTablesUpdated
方法被调用。然后,此方法要求每个特定的策略 AllTablesUpdated
方法。
When new bunch of data is received (new quotes, trades etc.) all strategies need to be updated asap. They need to recalculate their state/orders etc. When new bunch of data is ready to be read AllTablesUpdated
method is called. This method calls then AllTablesUpdated
method for each particular strategy.
public void AllTablesUpdated()
{
Console.WriteLine("Start updating all tables.");
foreach (Strategy strategy in strategies)
{
strategy.AllTablesUpdated();
}
Console.WriteLine("All tables updated in " + sw.ElapsedMilliseconds);
}
结果推迟。有时需要0或1毫秒(这是很不错的),有时它需要8-20毫秒,但有时它需要800毫秒。
The result defers. Sometimes it takes 0 or 1 milliseconds (that's very good), sometimes it takes 8-20 milliseconds, but sometimes it takes 800 milliseconds
有两个问题与当前实施
- 它使用一个线程,所以不使用多核心处理器
- 策略。 AllTablesUpdated()使用的共享资源,并且可以阻止了一段时间。如果某些特定的战略正在等待被释放的资源,所有其他的策略等待太(而不是可我们却不知何故推迟封锁战略,并开始处理其他策略?)
第二个瓶颈是非常相似的:
Second bottleneck is pretty similar:
private OrdersOrchestrator()
{
// A simple blocking consumer with no cancellation.
Task.Factory.StartNew(() =>
{
while (!dataItems.IsCompleted)
{
OrdersExecutor ordersExecutor = null;
// Blocks if number.Count == 0
// IOE means that Take() was called on a completed collection.
// Some other thread can call CompleteAdding after we pass the
// IsCompleted check but before we call Take.
// In this example, we can simply catch the exception since the
// loop will break on the next iteration.
try
{
ordersExecutor = dataItems.Take();
}
catch (InvalidOperationException) { }
if (ordersExecutor != null)
{
ordersExecutor.IssueOrders();
}
}
});
}
ordersExecutor
可以等待直到一些资源被释放。如果是这样所有其他 ordersExecutors
被封锁
ordersExecutor
may wait until some resources are released. If so all other ordersExecutors
are blocked.
在详细情况:每个策略
包含一个 ordersExecutor
,他们使用的是共享的资源。 strategy.AllTablesUpdated()
可等待资源被释放由它的 ordersExecutor
,反之亦然。如果这种情况发生时所有其他 stretegies / ordersExecutors
是太堵了。 。有超过100的策略
In details: each strategy
contains one ordersExecutor
, they are using shared resources. strategy.AllTablesUpdated()
may wait for resources to be released by it's ordersExecutor
and vice versa. If this condition occurs all others stretegies/ordersExecutors
are blocked too. There are more than 100 strategies.
如何修改代码来实现:
- 如果有一个
策略
或ordersExecutor
被阻止别人不应该被阻止? - 多核处理器和可能的多处理器平台? 的使用功率
- if one
strategy
orordersExecutor
is blocked others shouldn't be blocked? - use power of multi-core processors and probably multi-processors platform?
推荐答案
你的问题是相当广泛的,你基本上是问的是如何利用并行处理的应用程序?你已经有代码,被分解成离散的任务,因此使用并行不应该是一个大问题。我建议阅读有关PLINQ TPL和,既提供易于使用的API为这样的事情:
Your questions is rather broad, what you are basically asking is how to make use of parallelism in your application? You already have code which is broken up into discrete tasks, so using parallelism shouldn't be a big problem. I would recommend reading about PLinq and TPL, both provide easy-to-use APIs for this sort of thing:
的
这篇关于并联交易软件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!