运行嵌套的PLINQ查询是否有任何问题?
例如:
//Contains roughly 7000+ elements
mycollections.AsParallel().ForAll(x => {
//contains 12 elements
anothercollection.AsParallel().ForAll(y => {
//download some data from the web and parse it
});
});
Run Code Online (Sandbox Code Playgroud) 我有一个Model类的集合,我不想将每个类单独映射到View Model类.
我可以使用Parallel.ForEach或者ParallelEnumerable.ForAll,那么2,如果有的话有什么区别?
我在MSDN上找不到任何东西.
对于有i4o或PLINQ经验的人,我有一个问题.我有一个需要查询的大对象集合(大约400K).逻辑非常简单明了.例如,有Person对象的集合,我需要找到具有相同的名字,姓氏,datebirth,或名字/姓氏的首字母等相匹配的人它只是使用LINQ to对象耗时的过程.
我想知道i4o (http://www.codeplex.com/i4o)
或PLINQ可以帮助提高查询性能.哪一个更好?如果那里有任何方法.
谢谢!
我没有看到使用以下代码处理速度的任何改进:
IEnumerable<Quote> sortedQuotes = (from x in unsortedQuotes.AsParallel()
orderby (x.DateTimeTicks)
select x);
Run Code Online (Sandbox Code Playgroud)
顺序版本:
IEnumerable<Quote> sortedQuotes = (from x in unsortedQuotes
orderby (x.DateTimeTicks)
select x);
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么吗?我将源集合中的项目数量从数千个变为数千万个,并且没有大小显示并行版本即将发布.
任何提示赞赏.顺便说一句,如果有人知道更快速的排序方式(给定我指示的项目变量类型(包含项目在集合中排序的长DateTimeTicks),这也是值得欣赏的.
编辑:"有效排序" - >尽快.
谢谢
我正在尝试实现PLINQ示例但面临以下问题我的顺序查询比并行查询执行得更快.
这是代码示例:
Stopwatch sw = new Stopwatch();
int[] vals = Enumerable.Range(0, Int16.MaxValue).ToArray();
sw.Start();
int[] x1 = vals.Where(x => x % 2 == 0).ToArray();
sw.Stop();
Console.WriteLine("Sequential Execution {0} milliseconds", sw.ElapsedMilliseconds);
sw.Restart();
int[] x2 = vals.AsParallel().Where(x => x % 2 == 0).ToArray();
sw.Stop();
Console.WriteLine("Parallel Execution {0} milliseconds", sw.ElapsedMilliseconds);
Run Code Online (Sandbox Code Playgroud)
我的机器是Pentium(R)Dual-Core我也尝试过四核AMD Opteron(tm).
相同的结果并行查询比顺序查询运行慢.你能告诉我我的问题是什么吗?
谢谢.
假设我Particle在X,Y空间中有多个s,并且我想将它们全部归一化,使得平均X和Y为0.
串口实施:
public void Normalise()
{
double avgX = 0.0;
double avgY = 0.0;
foreach (Particle p in Particles)
{
avgX += p.X;
avgY += p.Y;
}
avgX /= (double)Particles.Count;
avgY /= (double)Particles.Count;
foreach (Particle p in Particles)
{
p.X -= avgX;
p.Y -= avgY;
}
}
Run Code Online (Sandbox Code Playgroud)
这是有效的,性能也不错,因为它是O(n),但它是"令人尴尬的平行".看看我的PLINQ实现:
public void PNormalise()
{
double avgX = 0.0;
double avgY = 0.0;
Particles.AsParallel().ForAll(p =>
{
avgX += p.X;
avgY += p.Y;
});
avgX /= (double)Particles.Count;
avgY /= (double)Particles.Count;
Particles.AsParallel().ForAll(p => …Run Code Online (Sandbox Code Playgroud) 我有2个集合都包含相同类型的对象,并且两个集合每个都有大约40K对象.
每个集合包含的对象的代码基本上就像一个字典,除了我重写了equals和hash函数:
public class MyClass: IEquatable<MyClass>
{
public int ID { get; set; }
public string Name { get; set; }
public override bool Equals(object obj)
{
return obj is MyClass && this.Equals((MyClass)obj);
}
public bool Equals(MyClass ot)
{
if (ReferenceEquals(this, ot))
{
return true;
}
return
ot.ID.Equals(this.ID) &&
string.Equals(ot.Name, this.Name, StringComparison.OrdinalIgnoreCase);
}
public override int GetHashCode()
{
unchecked
{
int result = this.ID.GetHashCode();
result = (result * 397) ^ this.Name.GetSafeHashCode();
return result;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我用来比较集合并获得差异的代码只是使用PLinq的简单Linq查询.
ParallelQuery p1Coll = …Run Code Online (Sandbox Code Playgroud) 我知道linq的错误使用可能会导致性能降低,但这次太奇怪了.
当我打电话"AsParallel.TakeWhile.AsParallel.ForAll",这是MUCH比"AsParallel.TakeWhile.ForAll"慢.有人可以解释一下原因吗?
using System;
using System.Diagnostics;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Action<int> doNothing = i => { };
var stopwatch = Stopwatch.StartNew();
Enumerable.Range(1, 100).AsParallel().TakeWhile(m => m < 10)
.ForAll(doNothing);
var ticks1 = stopwatch.ElapsedTicks;
stopwatch.Restart();
Enumerable.Range(1, 100).AsParallel().TakeWhile(m => m < 10)
.AsParallel() // spend much more time with this AsParallel
.ForAll(doNothing);
var ticks2 = stopwatch.ElapsedTicks;
Console.WriteLine("ticks without AsParallel: {0}\r\n with AsParallel: {1}", ticks1, ticks2);
//ticks without AsParallel: 87956
//with AsParallel: 6688708 …Run Code Online (Sandbox Code Playgroud) 我有一个二维字节数组,看起来像这样:
0 0 0 0 1
1 1 1 1 0
0 0 1 1 1
1 0 1 0 1
数组中的每个值只能是0或1.上面的简化示例显示了4行,每行有5列.我试图弄清楚如何使用LINQ将索引返回到具有最大数量1s的行,在上面的示例中应该返回1.
以下非LINQ C#代码解决了这个问题:
static int GetMaxIndex(byte[,] TwoDArray)
{
// This method finds the row with the greatest number of 1s set.
//
int NumRows = TwoDArray.GetLength(0);
int NumCols = TwoDArray.GetLength(1);
int RowCount, MaxRowCount = 0, MaxRowIndex = 0;
//
for (int LoopR = 0; LoopR < NumRows; LoopR++)
{
RowCount = 0;
for (int LoopC = 0; LoopC < …Run Code Online (Sandbox Code Playgroud) 我正在使用PLINQ,代码如下:
static void Main(string[] args)
{
var lt = new List<int>() {1,2,3,4,5};
try
{
var nlt = lt.AsParallel().Select(Test).ToList();
}
catch (AggregateException e)
{
foreach (var ex in e.InnerExceptions)
{
Console.WriteLine(ex.Message);
}
}
}
private static bool Test(int n)
{
if (n == 1)
{
Thread.Sleep(1000);
}
if (n == 3)
{
Thread.Sleep(3000);
}
if (n == 5)
{
Thread.Sleep(5000);
}
if (n == 2 || n == 4)
{
throw new Exception("New exception");
}
Console.WriteLine("element : {0}", n);
return true; …Run Code Online (Sandbox Code Playgroud)