高效地将项目添加到列表 <class> 并行 C#
Efficiently add items to a list<class> in parallel C#
我有功能代码,可以在 class 列表中拆分一个 属性 的字符串:由 string, string, string
组成的数据框。
现在我正在声明一个空的 Dataframe2 (string,string[], string
) 并使用 Add
将项目附加到列表中
class Program
{
public static string[] SPString(string text)
{
string[] elements;
elements = text.Split(' ');
return elements;
}
//Structures
public class Dataframe
{
public string Name { get; set; }
public string Text { get; set; }
public string Cat { get; set; }
}
public class Dataframe2
{
public string Name { get; set; }
public string[] Text { get; set; }
public string Cat { get; set; }
}
static void Main(string[] args)
{
List<Dataframe> doc = new List<Dataframe>{new Dataframe { Name = "Doc1", Text = "The quick brown cat", Cat = ""},
new Dataframe { Name = "Doc2", Text = "The big fat cat", Cat = "Two"},
new Dataframe { Name = "Doc4", Text = "The quick brown rat", Cat = "One"},
new Dataframe { Name = "Doc3", Text = "Its the cat in the hat", Cat = "Two"},
new Dataframe { Name = "Doc5", Text = "Mice and rats eat seeds", Cat = "One"},
};
// Can this be made more efficient?
ConcurrentBag<Dataframe2> doc2 = new ConcurrentBag<Dataframe2>();
Parallel.ForEach(doc, entry =>
{
string s = entry.Text;
string[] splitter = SPString(s);
doc2.Add(new Dataframe2 {Name = entry.Name, Text = splitter, Cat =entry.Cat});
} );
}
}
是否有更有效的方法使用并行 LINQ 将内容添加到列表中,其中 Dataframe2 继承了我没有修改的属性?
您可以尝试使用 PLinq 添加 并行性 并同时保留 List<T>
:
// Do NOT create and then fill the List<T> (which is not thread-safe) in parallel manually,
// Let PLinq do it for you
List<Dataframe2> doc2 = doc
.AsParallel()
.Select(entry => {
//TODO: make Dataframe2 from given Dataframe (entry)
...
return new Dataframe2 {Name = entry.Name, Text = splitter, Cat = entry.Cat};
})
.ToList();
我有功能代码,可以在 class 列表中拆分一个 属性 的字符串:由 string, string, string
组成的数据框。
现在我正在声明一个空的 Dataframe2 (string,string[], string
) 并使用 Add
class Program
{
public static string[] SPString(string text)
{
string[] elements;
elements = text.Split(' ');
return elements;
}
//Structures
public class Dataframe
{
public string Name { get; set; }
public string Text { get; set; }
public string Cat { get; set; }
}
public class Dataframe2
{
public string Name { get; set; }
public string[] Text { get; set; }
public string Cat { get; set; }
}
static void Main(string[] args)
{
List<Dataframe> doc = new List<Dataframe>{new Dataframe { Name = "Doc1", Text = "The quick brown cat", Cat = ""},
new Dataframe { Name = "Doc2", Text = "The big fat cat", Cat = "Two"},
new Dataframe { Name = "Doc4", Text = "The quick brown rat", Cat = "One"},
new Dataframe { Name = "Doc3", Text = "Its the cat in the hat", Cat = "Two"},
new Dataframe { Name = "Doc5", Text = "Mice and rats eat seeds", Cat = "One"},
};
// Can this be made more efficient?
ConcurrentBag<Dataframe2> doc2 = new ConcurrentBag<Dataframe2>();
Parallel.ForEach(doc, entry =>
{
string s = entry.Text;
string[] splitter = SPString(s);
doc2.Add(new Dataframe2 {Name = entry.Name, Text = splitter, Cat =entry.Cat});
} );
}
}
是否有更有效的方法使用并行 LINQ 将内容添加到列表中,其中 Dataframe2 继承了我没有修改的属性?
您可以尝试使用 PLinq 添加 并行性 并同时保留 List<T>
:
// Do NOT create and then fill the List<T> (which is not thread-safe) in parallel manually,
// Let PLinq do it for you
List<Dataframe2> doc2 = doc
.AsParallel()
.Select(entry => {
//TODO: make Dataframe2 from given Dataframe (entry)
...
return new Dataframe2 {Name = entry.Name, Text = splitter, Cat = entry.Cat};
})
.ToList();