Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.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循环之间有什么区别吗?_C#_.net_Performance - Fatal编程技术网

C# 这两个for循环之间有什么区别吗?

C# 这两个for循环之间有什么区别吗?,c#,.net,performance,C#,.net,Performance,从功能上讲,这两个循环没有区别,但是选择一个循环比另一个循环有什么好处吗 for (var i = 0; i < loopLimit; i++) { } 把较大的数字放在的左边有什么区别吗不,没有。但是你应该像每个人一样使用第一个问题。这个问题加上了标签,尽管从正文上看不清楚这是否是一个严格的性能问题,或者这两种风格之间可能存在的其他离题差异,例如可读性、通用约定等。总之,为了解决性能方面的问题,我曾经编写基准测试,在非迭代器操作数为常量和变量的情况下比较这两个运算符 使用Benchm

从功能上讲,这两个循环没有区别,但是选择一个循环比另一个循环有什么好处吗

for (var i = 0; i < loopLimit; i++)
{

}

把较大的数字放在
的左边有什么区别吗不,没有。但是你应该像每个人一样使用第一个问题。

这个问题加上了标签,尽管从正文上看不清楚这是否是一个严格的性能问题,或者这两种风格之间可能存在的其他离题差异,例如可读性、通用约定等。总之,为了解决性能方面的问题,我曾经编写基准测试,在非迭代器操作数为常量和变量的情况下比较这两个运算符

使用BenchmarkDotNet.Attributes;
名称空间SO58016813
{
公共静态类程序
{
公共静态void Main()
{
BenchmarkDotNet.Running.BenchmarkRunner.Run();
}
}
[ClrJob()]
[CoreJob()]
[分类列()]
[GroupBenchmarksBy(BenchmarkDotNet.Configs.BenchmarkLogicalGroupRule.ByCategory)]
公共课基准
{
private const int LoopLimit=int.MaxValue;
[基准(基线=真实)]
[基准类别(“常量操作数”)]
public void iteratorlessthanctant()
{
对于(变量i=0;ii;i++)
{
//什么也不做。。。
}
}
[基准(基线=真实)]
[基准类别(“变量操作数”)]
public void迭代器lessthanVariable()
{
var loopLimit=loopLimit;
对于(变量i=0;ii;i++)
{
//什么也不做。。。
}
}
}
}
我事先做了一点研究,以确保空循环不会被优化,而且据我所知,它们不会被优化。以下是我在.NETFramework和.NETCore上获得的结果

// * Summary *

BenchmarkDotNet=v0.11.5, OS=Windows 10.0.18362
Intel Core i7 CPU 860 2.80GHz (Nehalem), 1 CPU, 8 logical and 4 physical cores
.NET Core SDK=2.1.802
  [Host] : .NET Core 2.1.13 (CoreCLR 4.6.28008.01, CoreFX 4.6.28008.01), 64bit RyuJIT
  Clr    : .NET Framework 4.7.2 (CLR 4.0.30319.42000), 64bit RyuJIT-v4.8.4010.0
  Core   : .NET Core 2.1.13 (CoreCLR 4.6.28008.01, CoreFX 4.6.28008.01), 64bit RyuJIT


|                      Method |  Job | Runtime |       Categories |    Mean |    Error |   StdDev | Ratio |
|---------------------------- |----- |-------- |----------------- |--------:|---------:|---------:|------:|
|    IteratorLessThanConstant |  Clr |     Clr | Constant operand | 1.285 s | 0.0063 s | 0.0059 s |  1.00 |
| ConstantGreaterThanIterator |  Clr |     Clr | Constant operand | 1.282 s | 0.0021 s | 0.0020 s |  1.00 |
|                             |      |         |                  |         |          |          |       |
|    IteratorLessThanVariable |  Clr |     Clr | Variable operand | 1.288 s | 0.0065 s | 0.0061 s |  1.00 |
| VariableGreaterThanIterator |  Clr |     Clr | Variable operand | 1.282 s | 0.0028 s | 0.0026 s |  1.00 |
|                             |      |         |                  |         |          |          |       |
|    IteratorLessThanConstant | Core |    Core | Constant operand | 1.286 s | 0.0082 s | 0.0077 s |  1.00 |
| ConstantGreaterThanIterator | Core |    Core | Constant operand | 1.287 s | 0.0072 s | 0.0067 s |  1.00 |
|                             |      |         |                  |         |          |          |       |
|    IteratorLessThanVariable | Core |    Core | Variable operand | 1.284 s | 0.0063 s | 0.0059 s |  1.00 |
| VariableGreaterThanIterator | Core |    Core | Variable operand | 1.286 s | 0.0075 s | 0.0071 s |  1.00 |
基于以上结果

  • 如果你问一个接线员是否比另一个接线员快,我会说不
  • 如果你问应该使用哪种操作符,我会建议你认为哪种操作符能给你的代码带来最大的可读性和清晰性,因为这比任何微优化(如果存在的话)都重要得多

从功能上讲,不是。但是,第一种语言在C#、Java和C/C++中是惯用的。看到第二种情况的人可能会对为什么会这样做感到困惑,并且可能会因此寻找不同的行为,即使没有。如果
i
x>i
快,我会很惊讶的。我投票结束这个问题,可能是重复的,因为它是关于理解事物是如何书写的人性的。这与语法或性能无关。@JesseJohnson-你把注意力放在了错误的领域。如果你生产的产品需要350毫秒而不是340毫秒,你的客户不会在意。注意减速的地方(可能不是这个循环),测量和补救。不要试图学习/想象如何从他人那里构建高性能软件;特别是如果他们不太可能追求高性能。[需要引证]我明白,更常见的是看到它是以第一种样式
index
编写的,但我想知道为什么这会成为常态。也许你应该问一下。@JesseJohnson可能是因为这都是关于限制循环计数器的,所以很自然地把它放在第一位。伟大的文章@IanMercer链接!杰出的这太棒了。非常感谢。
void MeasureLoop()
{
    const int loopLimit = Int32.MaxValue;

    var timer = new Stopwatch();    
    timer.Start();

    for (var i = 0; i < loopLimit; i++)
    {
        //
    }

    timer.Stop();
}
void MeasureLoop()
{
    const int loopLimit = Int32.MaxValue;

    var timer = new Stopwatch();    
    timer.Start();

    for (var i = 0; loopLimit > i; i++)
    {
        //
    }

    timer.Stop();
}
// * Summary *

BenchmarkDotNet=v0.11.5, OS=Windows 10.0.18362
Intel Core i7 CPU 860 2.80GHz (Nehalem), 1 CPU, 8 logical and 4 physical cores
.NET Core SDK=2.1.802
  [Host] : .NET Core 2.1.13 (CoreCLR 4.6.28008.01, CoreFX 4.6.28008.01), 64bit RyuJIT
  Clr    : .NET Framework 4.7.2 (CLR 4.0.30319.42000), 64bit RyuJIT-v4.8.4010.0
  Core   : .NET Core 2.1.13 (CoreCLR 4.6.28008.01, CoreFX 4.6.28008.01), 64bit RyuJIT


|                      Method |  Job | Runtime |       Categories |    Mean |    Error |   StdDev | Ratio |
|---------------------------- |----- |-------- |----------------- |--------:|---------:|---------:|------:|
|    IteratorLessThanConstant |  Clr |     Clr | Constant operand | 1.285 s | 0.0063 s | 0.0059 s |  1.00 |
| ConstantGreaterThanIterator |  Clr |     Clr | Constant operand | 1.282 s | 0.0021 s | 0.0020 s |  1.00 |
|                             |      |         |                  |         |          |          |       |
|    IteratorLessThanVariable |  Clr |     Clr | Variable operand | 1.288 s | 0.0065 s | 0.0061 s |  1.00 |
| VariableGreaterThanIterator |  Clr |     Clr | Variable operand | 1.282 s | 0.0028 s | 0.0026 s |  1.00 |
|                             |      |         |                  |         |          |          |       |
|    IteratorLessThanConstant | Core |    Core | Constant operand | 1.286 s | 0.0082 s | 0.0077 s |  1.00 |
| ConstantGreaterThanIterator | Core |    Core | Constant operand | 1.287 s | 0.0072 s | 0.0067 s |  1.00 |
|                             |      |         |                  |         |          |          |       |
|    IteratorLessThanVariable | Core |    Core | Variable operand | 1.284 s | 0.0063 s | 0.0059 s |  1.00 |
| VariableGreaterThanIterator | Core |    Core | Variable operand | 1.286 s | 0.0075 s | 0.0071 s |  1.00 |