This article introduces two ways to perform parallel programming with a pricing library such as QLNet.

First we get an idea of the time required to do sequentially a series of time consuming operations, like a monte carlo simulation.

Second we present two C#.Net alternatives of multithreaded computations: Threadpool and TPL.

We measure time using a Timespan since the framework is 4.0 (StopWatch is not available with this version). This is a basic console project and QLNet is attached to the references with Nuget.

The calculation class

Here is a class that takes care of arranging the QLNet objects and exposing a Premium double property.

Sequential calculation

We will call the previous class sequentially in order to get an idea of the time needed to price this option through Monte Carlo engine. Let’s do it four times:

On my old 1 CPU, dual core machine, the total time is around 9 seconds.

Threadpool

Now let’s rely on System.Threading ThreadPool class which will help us to parallelise the calls to OptionPricing:

Now it takes 2.7 seconds.

TPL

Let’s see if TPL (Task Parallel Library) Parallel.For helps to improve that multitasking work:

Now the same operation takes around 2.3 seconds.

Below is the overall output:

Multithreaded pricing C#

 

 

Facebooktwittergoogle_plusredditlinkedinmail