包含2个以上列表的列表在循环中运行缓慢我可以使用线程来加快速度吗? [英] A list containing 2 more lists works slow in looping can I use thread to make this faster?

查看:81
本文介绍了包含2个以上列表的列表在循环中运行缓慢我可以使用线程来加快速度吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,其中列表包含另外2个列表,其中 WorkItem 集合包含大量记录,例如7,000,需要10分钟。有没有办法让它更快,以防它决定workItem类型,如果它是一个错误,任务或产品积压项目。请告诉我如何使循环更快。我们花了10分钟来循环7,000条记录我们可以使用线程来加快速度吗?

This is my code in which a list contains 2 more lists where the WorkItem collection contains a large number of records such as 7,000 it takes 10 min. Is there any way to make it faster and in case it's deciding the workItem type if it's a bug, task or product backlog item. Please tell me how to make the looping faster. It's taking 10 min to loop 7,000 records can we use threading to make it faster?

var workItemList = new List<WorkItemViewModel>();

           for (int i = 0; i < workItemCollection.Count; i++)
           {
               var workItem = workItemCollection[i];

               if (workItem.Type.Name == "Product Backlog Item")
               {
                   var model = new WorkItemViewModel()
                   {


                       FID = (workItem.WorkItemLinks.Count > 0) ? ((workItem.WorkItemLinks[0].LinkTypeEnd.Name.ToString() != "Child") ? workItem.WorkItemLinks[0].TargetId : 0) : 0,

                       ID = workItem.Id,
                       Name = workItem.Title,
                       State = workItem.State,

                       priorty = Convert.ToInt32(workItem.Fields["Priority"].Value),
                       //   Size =(int) workItem.Fields["Size"].Value ,
                       Size = Convert.ToInt32(workItem.Fields["Effort"].Value),

                       StoryPoints = Convert.ToInt32(workItem.Fields["Story Points"].Value),
                       DoneStatus = workItem.Fields["Done Status"].Value.ToString(),
                       StoryOwner = workItem.Fields["Story Owner"].Value.ToString(),
                       Assignedto = workItem.Fields["Assigned To"].Value.ToString(),
                       StoryAuthor = workItem.Fields["Story Author"].Value.ToString(),
                       IterationPath = workItem.IterationPath
                   };

                   workItemList.Add(model);
               }
               else
               {
                   switch (workItem.Type.Name)
                   {
                       case "Task":
                           var task = new TFSTask()
                           {
                               Storyid = (workItem.WorkItemLinks.Count > 0) ? workItem.WorkItemLinks[0].TargetId : 0,
                               ID = workItem.Id,
                               name = workItem.Title,
                               //activity = workItem.Fields["MyCompany.Activity"].Value.ToString(),
                               //start = (DateTime?)workItem.Fields["MyCompany.ActivityStart"].Value,
                               //due = (DateTime?)workItem.Fields["MyCompany.ActivityFinish"].Value,
                               status = workItem.State,
                               IterationPath = workItem.IterationPath,
                               Assignedto = workItem.Fields["Assigned To"].Value.ToString(),

                               priorty = Convert.ToInt32(workItem.Fields["Priority"].Value),
                               effort = Convert.ToInt32(workItem.Fields["effort"].Value),
                               Completed = Convert.ToInt32(workItem.Fields["Completed"].Value)

                           };
                           if (task.Storyid != 0)
                           {
                               workItemList.Last().Tasks.Add(task);
                           }


                           break;
                       case "Bug":
                           var bug = new TFSIssue()
                           {
                               Storyid = (workItem.WorkItemLinks.Count > 0) ? workItem.WorkItemLinks[0].TargetId : 0,
                               ID = workItem.Id,
                               Name = workItem.Title,
                               //start = (DateTime?)workItem.Fields["MyCompany.ActivityStart"].Value,
                               //due = (DateTime?)workItem.Fields["MyCompany.ActivityFinish"].Value,
                               State = workItem.State,
                               IterationPath = workItem.IterationPath,
                               Assignedto = workItem.Fields["Assigned To"].Value.ToString(),

                               priorty = Convert.ToInt32(workItem.Fields["Priority"].Value),
                               effort = Convert.ToInt32(workItem.Fields["effort"].Value),
                               // Completed = Convert.ToInt32(workItem.Fields["Completed"].Value)
                           };
                           if (bug.Storyid != 0)
                           {
                               workItemList.Last().Issues.Add(bug);
                           }
                           break;



                       default:
                           break;
                   }
               }
           }

public class WorkItemViewModel
   {
       public string Name { get; set; }
       public int ID { get; set; }

       public string State { get; set; }
       // public DateTime? due { get; set; }
       public int priorty { get; set; }
       public int Size { get; set; }
       //  public int effort { get; set; }
       public int StoryPoints { get; set; }
       public string DoneStatus { get; set; }
       public string StoryOwner { get; set; }
       public string Assignedto { get; set; }
       public string StoryAuthor { get; set; }
       public string IterationPath { get; set; }
       public int FID { get; set; }
       public List<TFSIssue> Issues { get; set; }
       public List<TFSTask> Tasks { get; set; }

       public WorkItemViewModel()  // Added a public constructor
       {
           Issues = new List<TFSIssue>();
           Tasks = new List<TFSTask>();

       }

   }





我是什么尝试过:



包含2个以上列表的列表在循环中运行缓慢我可以使用Thread来加快速度吗?



What I have tried:

A list containing 2 more lists works slow in looping can i use Thread to make this faster?

推荐答案

(workItem.WorkItemLinks[0].LinkTypeEnd.Name.ToString()

为什么在已经是字符串的成员上调用 ToString()?这个问题也适用于其他 ToString()代码中的用法。



Why calling ToString() on a member which is already a string? This question also holds for other ToString() usages in your code.

Size = Convert.ToInt32(workItem.Fields["Effort"].Value)

转换类显示与内置解析函数相比性能非常差。始终使用 Parse TryParse 当你想从字符串中获取整数值时的方法。例如,上面的代码片段可以替换为

Convert class shows really poor performance compared to built-in parsing functions. Always use Parse or TryParse methods when you want to get an integral value from a string. For example, above snippet could be replaced with

Size = int.Parse(workItem.Fields["Effort"].Value)



此外,如果价值已经是 int ,根本不需要解析。不知道你的 WorkItemViewModel 类的定义,这只是一个假设。



我不是说你的代码无法从多线程处理中受益,但如果性能是您的关注,那么更好地处理基本操作,避免不必要的 ToString 调用和转换的用法上课。



我的2美分。希望这会有所帮助。


Furthermore, if Value would already be of type int, there would not be any need for parsing at all. Not knowing the definition of your WorkItemViewModel class, this is just an assumption.

I am not saying that your code could not benefit from a multithreaded treatment, but if performance is your concern, then better handle basic operations avoiding unnecessary ToString calls and usages of Convert class.

Just my 2 cents. Hope this helps.


如果您认真了解.NET中的时间性能,我建议您学习使用以下开源工具之一:BenchmarkDotNet(我使用的是什么)[ ^ ],或AppMetrics [ ^ ]。通过这样做,您将成为更好的程序员。 Scott Hanselman在2016年撰写了关于BenchMark的文章:[ ^ ]。这两个项目都得到了积极的维护。



如果不了解数据库的结构,我们能够帮助你的是imho,仅限于(优秀)建议的种类那个phil.o发布了。



假设你无法修改数据库,我看到的最大问题是你正在创建新对象逐个... piece 而不是将数据表直接映射到包含记录到对象实例集合的Class实例。



看一看在本CP文章中的概述:[ ^ ]



探索Linq中可能相关的功能:[ ^ ]
If you are serious about understanding time performance in .NET, I suggest you learn to use one of these open-source tools: BenchmarkDotNet (what I use) [^], or, AppMetrics [^]. By doing this, you will become a better programmer. Scott Hanselman wrote about BenchMark in 2016: [^]. Both these projects are actively maintained.

Without understanding the structure of the database here, our ability to help you is, imho, limited to the kinds of (excellent) suggestions that phil.o posted.

Assuming you can't modify the database, the biggest problem I see is that you are creating new objects piece-by-piece instead of mapping a datatable directly to an instance of a Class that holds a collection of instances of your record-to-object.

Take a look at this CP article for an overview: [^]

Explore Features in Linq that may be relevant: [^]


我没有看到任何明显的东西。

您需要了解花费的时间和方式,选择的工具是剖析器。

分析(计算机编程) - 维基百科 [ ^ ]

性能分析工具列表 - 维基百科 [ ^ ]



[更新]

I see nothing obvious.
You need to understand where and how time is spent, the tool of choice is a profiler.
Profiling (computer programming) - Wikipedia[^]
List of performance analysis tools - Wikipedia[^]

[Update]
引用:

在循环之前和之后放了秒表,显示10分钟

have put stopwatch before and after the loop and it show's 10 min



'了解时间花费'意味着测量g花在每行代码上的时间,以了解哪一个消耗最多的时间。


'Understanding how time is spend' means measuring time spend on each line of code to understand which one consume the most time.


这篇关于包含2个以上列表的列表在循环中运行缓慢我可以使用线程来加快速度吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆