What Parallels can do for you…

Tags: Parallels

I thought I would share a quick sample of what .NET Parallels can do for you. In this case I wanted to be able to go out and pull back entire tables and save a copy of the tables locally. In this example I put together the timings of how long it takes to pull back a collection of the data from the table, and with using Parallels in .NET the amount of time to accomplish the same task was cut in over half the time.

The Base Logic

I first create a Task that can be used by both the Synchronous and Asynchronous call. This method will use LinqToSql to get a full list of everything in the table.

public static void DoTask(IGetData getData)
{    
    var startTime = DateTime.Now;
    getData.GetList();
    var endTime = DateTime.Now;

    var timeSpan = endTime - startTime;
    Console.WriteLine(getData.Name + " took " + timeSpan.TotalSeconds + " seconds long");
}


To get the data I created an interface and implemented GetList generically

public abstract class GetData<TEntity> where TEntity : class
{    
    public IList GetList()
    {
        var db = new MyDataContext();
        return db.GetTable<TEntity>().Select(ca => ca).ToList();
    }
}

public interface IGetData
{
    string Name { get; }
    IList GetList();
}

Sample Implementation of the IGetData interface

public class GetContactNumber : GetData<ContactNumber> IGetData
{
    public string Name
    {
        get { return "ContactNumber"; }
    }
}

Parallel Test

In this test I have it call the DoTask for each table, I track when I started and when I finish to get a pretty good idea of how long it took. It will call the Parallel.ForEach method on the generic list of IGetData implementations.

private static void ParallelTest()
{
    Console.WriteLine("Parallels ---------------");
    var getDataList = new List<IGetData>
                        {
                              new GetContactAddresses(),
                              new GetContactName(),
                              new GetContactNumber(),
                              new GetTransactions(),
                              new GetDetail()
                        };

    var startTime = DateTime.Now;
    Parallel.ForEach(getDataList, DoTask);
    var endTime = DateTime.Now;
    var timeSpan = endTime - startTime;

    Console.WriteLine("Parallels took " + timeSpan.TotalSeconds + " seconds long");
}

Synchronous Test

In this test I will call the DoTask for each table in synchronous order, I track when the tasks begin and when I finish to get a good idea of how long it took.

private static void SynchronousTest()
{
    Console.WriteLine("Synchronous");
    var startTime = DateTime.Now;

    DoTask(new GetContactAddresses());
    DoTask(new GetContactName());
    DoTask(new GetContactNumber());
    DoTask(new GetTransactions());
    DoTask(new GetDetail());

    var endTime = DateTime.Now;
    var timeSpan = endTime - startTime;

    Console.WriteLine("Synchronous took " + timeSpan.TotalSeconds + " seconds long");

    Console.WriteLine();
}

Final Results

Finally I executed the program to see my results, and to my happiness I saved a huge amount of time by letting Parallel run each of my select statements at the same time across multiple threads.

Synchronous took 70 seconds
Asynchronous (Parallels) took 26 seconds

image

Add a Comment