我試圖使用Parallel.ForEach和ConcurrentBag更快地執行代碼,但它仍然運行的時間很長(特別是在考慮到在我的場景中我也可能是1.000.000 ++ ):修改列表中的項目<T> fast
List<Point> points = new List<Point>();
for(int i = 0; i<100000;i++) {
Point point = new Point {X = i-50000, Y = i+50000, CanDelete = false};
points.Add(point);
}
foreach (Point point in points) {
foreach (Point innerPoint in points) {
if (innerPoint.CanDelete == false && (point.X - innerPoint.X) < 2) {
innerPoint.Y = point.Y;
point.CanDelete = true;
}
}
}
描述你想達到什麼目的。即使對於N> = 20000,O(N^2)也太多了。 – 2011-04-30 19:31:51
如果你真的要在你的集合中有超過一百萬個項目,你可能想要開始尋找比嵌套循環更好的搜索算法......是否將它分散到少數幾個並行化的核心,它仍然會是10^12次迭代,這是很多的(你會意識到,這將刪除,例如,一個點的線,無論多長時間,只要點是彼此足夠接近,對吧?) – 2011-04-30 19:32:22
我需要在每個_X_上使用__ __ __ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _點所有點,而_X_可以與x 1-x 2
2011-04-30 19:37:57