我想我可能会在这里插话......
虽然我同意 不可变 结构在很多情况下都有意义,但可读性和维护性也应始终考虑到您的考虑因素。如果性能是您的目标,那么在 Linq Select 中创建新结构不会叠加(在这种情况下)。您只是在执行更多分配并产生更多垃圾。
在任何正常情况下,这可能都无关紧要。但是,“如果”你对性能感兴趣并且你有指令和循环OCD,那么我会考虑替代方案并对你的解决方案进行基准测试。
给出以下3种方法
public void ByRef(ref Rect entity)
{
if (entity.Y + entity.Height > 800)
{
entity.Y++;
}
}
public Rect ByImutableReturn(Rect entity)
{
return entity.Y + entity.Height > 800 ? new Rect(entity.Height, entity.Y + 1) : entity;
}
public Rect ByReturn(Rect entity)
{
if (entity.Y + entity.Height > 800)
{
entity.Y++;
}
return entity;
}
LinqImutableReturn
protected override List<Rect> InternalRun()
{
return Input.Cubes = Input.Cubes.Select(r => Input.ByImutableReturn(r))
.ToList();
}
LinqReturn
protected override List<Rect> InternalRun()
{
return Input.Cubes = Input.Cubes.Select(r => Input.ByReturn(r))
.ToList();
}
ForLoopByRef
protected override List<Rect> InternalRun()
{
for (var index = 0; index < Input.Cubes.Count; index++)
{
var t = Input.Cubes[index];
Input.ByRef(ref t);
Input.Cubes[index] = t;
}
return Input.Cubes.ToList();
}
ForLoopImutableReturn
protected override List<Rect> InternalRun()
{
for (var index = 0; index < Input.Cubes.Count; index++)
{
Input.Cubes[index] = Input.ByImutableReturn(Input.Cubes[index]);
}
return Input.Cubes.ToList();
}
ForLoopReturn
protected override List<Rect> InternalRun()
{
for (var index = 0; index < Input.Cubes.Count; index++)
{
Input.Cubes[index] = Input.ByReturn(Input.Cubes[index]);
}
return Input.Cubes.ToList();
}
结果
Mode : Release
Test Framework : .NET Framework 4.7.1
Benchmarks runs : 100 times (averaged/scale)
Scale : 10,000
Name | Time | Range | StdDev | Cycles
--------------------------------------------------------------------------
ForLoopByRef | 0.073 ms | 0.001 ms | 0.07 | 244,964
ForLoopReturn | 0.097 ms | 0.006 ms | 0.05 | 332,372
ForLoopImutableReturn | 0.116 ms | 0.003 ms | 0.08 | 388,188
LinqImutableReturn | 0.325 ms | 0.007 ms | 0.25 | 1,117,130
LinqReturn | 0.347 ms | 0.002 ms | 0.07 | 1,195,351
Scale : 100,000
Name | Time | Range | StdDev | Cycles
---------------------------------------------------------------------------
ForLoopByRef | 0.635 ms | 0.168 ms | 0.11 | 2,215,066
ForLoopImutableReturn | 0.867 ms | 0.175 ms | 0.10 | 3,027,096
ForLoopReturn | 0.890 ms | 0.225 ms | 0.09 | 3,109,831
LinqReturn | 2.957 ms | 0.166 ms | 0.17 | 10,347,672
LinqImutableReturn | 3.084 ms | 0.219 ms | 0.40 | 10,780,304
Scale : 1,000,000
Name | Time | Range | StdDev | Cycles
-----------------------------------------------------------------------------
ForLoopByRef | 6.624 ms | 1.685 ms | 0.83 | 23,156,409
ForLoopImutableReturn | 9.574 ms | 1.678 ms | 0.82 | 33,503,375
ForLoopReturn | 9.811 ms | 2.290 ms | 0.86 | 34,324,963
LinqImutableReturn | 32.463 ms | 1.401 ms | 1.11 | 113,246,111
LinqReturn | 32.973 ms | 0.830 ms | 1.18 | 114,892,311
总结
我会对这些结果持保留态度。当您查看高性能代码时,事情并不是那么干净。但是这个故事的寓意是,如果您要追求性能而不是可读性和可维护性,则需要在实际情况下对代码进行基准测试。