Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C#中的列表中查找它们之间相似的项,并嵌套For循环和Linq_C#_Linq_Loops_For Loop_Nested - Fatal编程技术网

在C#中的列表中查找它们之间相似的项,并嵌套For循环和Linq

在C#中的列表中查找它们之间相似的项,并嵌套For循环和Linq,c#,linq,loops,for-loop,nested,C#,Linq,Loops,For Loop,Nested,我有下面的代码工作正常,它在列表中找到彼此相似的项目±2。我想有两个版本来检查哪一个运行更快 版本a)带有嵌套For循环,如下代码所示。但是,代码的最后一部分有一个!我希望用另一个For循环替换List.Contains(),因为Contains()增加了4500个刻度,而其他两个嵌套的For循环只需要1500个刻度。因此,如果有人能帮助用另一个for循环替换Contains(),并提供相同的结果,我将不胜感激 版本b)相同,但使用LINQ 在两个版本中,输出列表intTestResult中的项

我有下面的代码工作正常,它在列表中找到彼此相似的项目±2。我想有两个版本来检查哪一个运行更快

版本a)带有嵌套For循环,如下代码所示。但是,代码的最后一部分有一个!我希望用另一个For循环替换List.Contains(),因为Contains()增加了4500个刻度,而其他两个嵌套的For循环只需要1500个刻度。因此,如果有人能帮助用另一个for循环替换Contains(),并提供相同的结果,我将不胜感激

版本b)相同,但使用LINQ

在两个版本中,输出列表intTestResult中的项目必须是:(1、2、8、9、10、12)

int intOffset=2;
List intTest=新列表{1,2,5,8,9,10,12,15,19,24};
List intTestResult=新列表();
var S1=Stopwatch.StartNew();
for(inta=0;a=int2&&int1-intOffset替换:

if (!intTestResult.Contains(int1))                      
    intTestResult.Add(int1);

bool contains=false;
for(intc=0;c
更换:

if (!intTestResult.Contains(int1))                      
    intTestResult.Add(int1);

bool contains=false;
for(intc=0;c
担心你衡量效率的方式是错误的

  • 首先,您应该多次执行方法,例如在循环中。因为第一次执行总是需要更多的时间,并且您可能可以从计算中排除第一个循环

  • 使用大量数据进行处理。许多有效的数据结构在处理小数据时速度较慢,但在处理大数据时速度非常快

  • 如果您的应用程序无法处理大量数据,那么您根本不需要测试性能。只需编写其他开发人员(您自己)易于阅读和理解的代码。。我会说“编写可以由人脑快速处理的代码”

  • 使用
    HashSet
    可枚举.聚合方法

    // Helper method to check if two numbers within offset
    private IEnumerable<int> WithinOffset(int? previous, int current, int offset)
    {
        if (previous.HasValue == false)
        {
            yield break;
        }
    
        var difference = Math.Abs(previous.Value, current);
        if (difference > 0 && difference <= offset)
        {
            yield return previous.Value;
            yield return current;
        }
    }
    
    var clock = Stopwatch.Start();
    
    var offset = 2;
    var result = 
        givenData.OrderBy(number => number)
                 .Aggregate(
                     (All: Enumerable.Empty<int>(), Last: default(int?)),
                     (summary, current) => 
                     {
                         var withinOffset = WithinOffset(summery.Last, current, offset);
                         var all = summary.All.Concat(withinOffset);
                         return (all, current);
                     },
                     (summary) => summary.All.ToHashSet().ToList());
    clock.Stop();
    
    var ticks = clock.ElapsedTicks;
    

    担心你衡量效率的方式是错误的

  • 首先,您应该多次执行方法,例如在循环中。因为第一次执行总是需要更多的时间,并且您可能可以从计算中排除第一个循环

  • 使用大量数据进行处理。许多有效的数据结构在处理小数据时速度较慢,但在处理大数据时速度非常快

  • 如果您的应用程序无法处理大量数据,那么您根本不需要测试性能。只需编写其他开发人员(您自己)易于阅读和理解的代码。。我会说“编写可以由人脑快速处理的代码”

  • 使用
    HashSet
    可枚举.聚合方法

    // Helper method to check if two numbers within offset
    private IEnumerable<int> WithinOffset(int? previous, int current, int offset)
    {
        if (previous.HasValue == false)
        {
            yield break;
        }
    
        var difference = Math.Abs(previous.Value, current);
        if (difference > 0 && difference <= offset)
        {
            yield return previous.Value;
            yield return current;
        }
    }
    
    var clock = Stopwatch.Start();
    
    var offset = 2;
    var result = 
        givenData.OrderBy(number => number)
                 .Aggregate(
                     (All: Enumerable.Empty<int>(), Last: default(int?)),
                     (summary, current) => 
                     {
                         var withinOffset = WithinOffset(summery.Last, current, offset);
                         var all = summary.All.Concat(withinOffset);
                         return (all, current);
                     },
                     (summary) => summary.All.ToHashSet().ToList());
    clock.Stop();
    
    var ticks = clock.ElapsedTicks;
    

    如果你想让我们看的话,你需要给我们看LINQ版本。
    if(Math.Abs(int1-int2)@Jim Mischel
    if(Math.Abs(int1-int2)你测试的整数数组有多大?@Fabio这个列表是20个小项目。如果你想让我们看的话,你需要给我们看LINQ版本。
    if(Math.Abs(int1-int2)@Jim Mischel
    if(Math.Abs(int1-int2)您测试的整数数组有多大?@Fabio该列表只有20个小项目。非常感谢!它用大约1500个刻度完成所有计算。比使用列表快得多。Contains()非常感谢!它用大约1500个刻度完成所有计算。比使用列表快得多。Contains()谢谢。我确实试过,但比!List.Contains()稍慢。大约需要8000个刻度。@Roblogic,哈希集对于更大的数据量要快得多。检查更新的应答谢谢。我确实试过,但比!List.Contains()稍慢。大约需要8000个滴答声。@Roblogic,哈希集对于更大的数据量要快得多。请检查更新的答案
    
    var template = new[] { 1, 2, 5, 8, 9, 10, 12, 15, 19, 24 }
    var givenData = 
        Enumerable.Range(0, 100)
                  .Select(i => i * 100)
                  .Select(i => template.Select(number => number + i))
                  .SelectMany(number => number)
                  .ToList();
    
    
    // Approach with accepted answer
    // Elapsed ticks = 11517000 (1.1517 seconds)
    
    // Approach with HashSet, OrderBy and Aggregate
    // Elapsed ticks = 2202000 (0.2202 seconds)