I have following piece of code
List<GridVM> _itemsSource = new List<GridVM>();
foreach(var shelf in Network.Shelves)
{
foreach(var equipment in shelf.Equipment)
{
var gridVM= new GridVM(equipment);
itemSource.Add(gridVM);
}
}
here _itemSource is a collection that is going to be data source for a grid.
now creating each vm object for each equipment is taking little bit time ~~around 8 seconds. I want to speed up grid data source population using TPL by running inner forloop in different thread and add the vm to the main collection of _itemSource.
How to achieves so using TPL. Will it really speed up my job considering the facts thread overheads and locking overheads. I can convert current list item source to ConcurrentList or ConcurrntBag. but same question :-will it really give any boost or not. if not, then I am interest to know why??
You can easily parallelize your code using PLINQ (aka Parallel Linq):
var _itemsSource = Network.Shelves
.SelectMany(s => s.Equipment)
.AsParallel()
.Select(e => new GridVM(e))
.ToList();
It may execute faster if the constructor takes time. If the overhead is just "adding items to a list" then you'll gain nothing. That said, if it takes 8 seconds just to add items to the list, then you definitely have other problems, such as memory consumption.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.