并行 Linq 到对象集合
当我尝试使用 Plinq (并行 linq )进行对象收集时,我遇到了一个基本问题,我发现 Plinq 与正常操作在执行时间方面没有太大差异。任何人都可以检查我的代码并告诉我为什么会发生这种情况。我已经在 i7 处理器中运行了这段代码。
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; }
}
上述代码的结果为
并行模式下已用时间:6:411 秒:毫秒 正常
模式下已用时间:6:68 秒:毫秒
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Where() 返回 IEnumerable 并且不会导致查询被评估。您需要明确地评估答案(例如,使用 ToList())。
启动线程时需要考虑一些开销,因此您的工作负载必须花费足够的时间来执行,以便您可以观察到差异。对于适合内存的列表,过滤可能不够,除非评估标准的成本很高。
使用 System.Diagnostics.Stopwatch 类进行测量;它具有更高的精度。
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.