・class → structへ変換
・AsParallel() / Linqの利用
・Capacity / Listの利用
・foreach() → AsParallel().ForAll()へ変換
・foreach() → Parallel.ForEach()へ変換
・List<ClassItem> → ConcurrentBag<ClassItem>へ変換
で実験。
1 2 3 4 5 6 7 8 |
List<string> tmpList = new List<string>(RawItems); tmpList.Capacity = RawItemCount; foreach (string tmpString in needle.Split('|')) { tmpList = tmpList.AsParallel().Where(x => x.ToLower().Contains(tmpString.ToLower())).ToList(); } items = tmpList.Take(VisibleCount); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
List<ItemClass> itemClassList = new List<ItemClass>(RawItemCount); foreach (var item in items) { itemClassList.Add(new ItemClass() { DisplayItemPath = Path.GetFileName(item) + " . . . ■" + Path.GetDirectoryName(item), ItemPath = item }); } //var obj = new object(); //items.AsParallel().ForAll(item => { // lock (obj) // { // itemClassList.Add(new ItemClass() // { // DisplayItemPath = Path.GetFileName(item) + " . . . ■" + Path.GetDirectoryName(item), // ItemPath = item // }); // } //}); //var obj = new object(); //System.Threading.Tasks.Parallel.ForEach(items, item => //{ // lock (obj) // { // itemClassList.Add(new ItemClass() // { // DisplayItemPath = Path.GetFileName(item) + " . . . ■" + Path.GetDirectoryName(item), // ItemPath = item // }); // } //}); |
1 2 3 4 5 6 7 8 9 10 11 12 |
var itemClassList = new System.Collections.Concurrent.ConcurrentBag<ItemClass>(); var obj = new object(); System.Threading.Tasks.Parallel.ForEach(items, item => { itemClassList.Add(new ItemClass() { DisplayItemPath = Path.GetFileName(item) + " . . . ■" + Path.GetDirectoryName(item), ItemPath = item }); }); |