在回答其中一个问题时,我看到了2个LINQ代码的例子,它们应该完全相同.但我对性能感到惊讶,并发现一个代码比另一个代码快得多.我无法理解为什么.
我从问题中获取了数据结构
public struct Strc
{
public decimal A;
public decimal B;
// more stuff
}
public class CLASS
{
public List<Strc> listStrc = new List<Strc>();
// other stuff
}
Run Code Online (Sandbox Code Playgroud)
然后我写了简单的基准测试(使用了benchmarkdotnet库)
UPD我包括了所有要求的测试
public class TestCases
{
private Dictionary<string, CLASS> dict;
public TestCases()
{
var m = 100;
var n = 100;
dict = Enumerable.Range(0, m)
.Select(x => new CLASS()
{
listStrc = Enumerable.Range(0, n)
.Select(y => new Strc() { A = y % 4, B …
Run Code Online (Sandbox Code Playgroud)