Reputation: 335
I got a basic question when I tried with Plinq (Parallel linq ) to object collection and I observed that Plinq Vs normal operation does not have much difference in terms of execution time. Could anybody can check my code and advice me why so happening. I have run this code in i7 processor.
class Program
{
static void Main(string[] args)
{
new Program().Plinq();
new Program().linq();
Console.ReadLine();
}
void Plinq()
{
DateTime startTime = DateTime.Now;
var query1 = (from port in new XpressEntities().Portfolios.Take(1000000)
select new port { PortId = port.PORT_ID, CFAC = port.CFAC }).ToList<port>();
query1.AsParallel().Where(e => e.PortId == 0);
TimeSpan ts = DateTime.Now.Subtract(startTime);
Console.WriteLine("Time Elapsed: {0} Seconds:MilliSeconds in Paralel mode", ts.Seconds + ":" + ts.Milliseconds);
}
void linq()
{
DateTime startTime = DateTime.Now;
var query1 = (from port in new XpressEntities().Portfolios.Take(1000000)
select new port { PortId = port.PORT_ID, CFAC = port.CFAC }).ToList<port>();
query1.Where(e => e.PortId == 0);
TimeSpan ts = DateTime.Now.Subtract(startTime);
Console.WriteLine("Time Elapsed: {0} Seconds:MilliSeconds in Normal mode", ts.Seconds + ":" + ts.Milliseconds);
}
}
class port
{
public int PortId { get; set; }
public string CFAC { get; set; }
}
Result of above code is
Time Elapsed: 6:411 Seconds:MilliSeconds in Paralel mode
Time Elapsed: 6:68 Seconds:MilliSeconds in Normal mode
Upvotes: 1
Views: 938
Reputation: 1904
Where() returns an IEnumerable and does not result in the query being evaluated. You need to explicity evaluate the answer (for example, using ToList()).
There is some overhead in starting up threads which must be taken account of, so your work load must take sufficient time to execute that you can observe a difference. Filtering may not be enough on a list which will fit in memory unless the criteria are expensive to evaluate.
Use the System.Diagnostics.Stopwatch class for your measurements; it has much better precision.
Upvotes: 1