在函数调用中实例化对象的性能(解释?)

Performance of instantiating objects in function call (Explanation?)

谁能给我解释一下这里的性能差异。对于背景,我在 Leetcode 和我的原始代码(底部)上做一个回溯问题,我在其中创建新列表内联函数调用执行时间约为 490 毫秒,但更改后的代码(顶部)在添加列表之前重新创建列表执行时间约为 256 毫秒。几乎快两倍?谁能向我解释为什么会这样,我不知道这是 compiler/optimization 问题还是我遗漏了什么。

private void backtracking(List<IList<int>> retList, List<int> tempList, int k, int n, int start) {
        if(tempList.Count==k) {
            List<int> combo = new List<int>(tempList); //*** <- faster with this line ***
            retList.Add(combo);
            return;
        }

        for(var i=start; i<n; i++) {
            tempList.Add(i+1);
            backtracking(retList, tempList, k, n, i+1);
            tempList.RemoveAt(tempList.Count-1);
        }
    }
private void backtracking(List<IList<int>> retList, List<int> tempList, int k, int n, int start) {
        if(tempList.Count==k) {
            retList.Add(tempList);
            return;
        }

        for(var i=start; i<n; i++) {
            tempList.Add(i+1);
            backtracking(retList, new List<int>(tempList), k, n, i+1); //*** <- Slower with this line ***
            tempList.RemoveAt(tempList.Count-1);
        }
    }

在最上面的解决方案中,您在 if 中实例化了一个列表。

在底部的解决方案中,每次循环遍历 for 时都会实例化一个新列表。

实例化很昂贵。