如何使用LINQtoObjects安排非重叠作业?这是另一个资源分配问题。我的目标是运行一个查询,将任何时隙的最高优先级作业分配给两个CPU内核之一(只是一个例子,所以让我们假设没有中断或多任务处理)。注意:这与我之前关于分区的帖子类似,但侧重于重叠时间和分配多个项目,而不仅仅是最高优先级的项目。这是我们的目标:publicclassJob{publicintId;公共int优先级;公共日期时间开始;公共日期时间结束;}真实的数据集非常大,但对于这个例子,假设有1000个作业要分布在两个CPU核心之间。它们都被加载到内存中,我需要对它们运行一个LINQtoObjects查询。目前这需要将近8秒和140万次比较。我正在利用这篇文章中引用的逻辑来确定两个项目是否重叠,但与那篇文章不同的是,我不仅需要找到重叠的项目,还需要排列任何重叠集合的顶部项目,然后排列下一个项目。在我开始编写代码之前,让我指出当前无效算法的步骤:确定剩余的作业(通过删除任何已分配的作业)优先核心。通过执行查询加入新作业对于每个剩余的核心,重复从停止1开始的问题:完整示例代码:publicclassJob{publicstaticlongIterations;公共int标识;公共int优先级;公共日期时间开始;公共日期时间结束;publicboolOverlaps(Jobother){Iterations++;返回this.End>other.Begin&&this.Beginother.Begin&&this.BeginMemberspublicintCompareTo(Assignmentother){returnJob.Begin.CompareTo(other.Job.Begin);}#endregion}classProgram{staticvoidMain(string[]args){constintJobs=1000;constint核心=2;constintConcurrentJobs=核心数+1;constint优先级=核心数+3;日期时间startTime=newDateTime(2011,3,1,0,0,0,0);Console.WriteLine(string.Format("{0}作业x{1}核",作业,核));vartimer=Stopwatch.StartNew();Console.WriteLine("填充数据");varjobs=newList();for(intjobId=0;jobId[Cores];for(intcore=0;core();Job[]lastJobs=newJob[Cores];foreach(Jobjinjobs){Jobjob=j;boolassigned=false;for(intcore=0;corelastJobs[core].Priority){//重叠和高r优先级,所以我们替换Jobtemp=lastJobs[core];lastJobs[核心]=工作;工作=临时工;//稍后会尝试分配给其他核心作业[core].Add(newAssignment{Job=job,Core=core});分配=真;休息;}}if(!assigned){//TODO:如果没有分配怎么办?您的代码似乎只是忽略了它们}}Listmerged=newList();for(intcore=0;coreassignments2=null;for(intcore=0;corea.Job.Begin).Take(10)){Console.WriteLine(string.Format("{0}-{1}Id{2}P{3}C{4}",assignment.Job.Begin,assignment.Job.End,assignment.Job.Id,assignment.Job.Priority,assignment.Core));}if(merged.Count!=assignments2.Count())System.Console.WriteLine("差异计数{0},{1}",merged.Count,assignments2.Count());for(inti=0;i由于严重错误删除重做...:PC#学习教程就是这些:如何使用LINQtoObjects安排非重叠作业?如果所有分享的内容对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: