如果 ValueTuple 由于其值类型性质而有太多"fields",那么 ValueTuple 是否是一个糟糕的选择?



由于我决定用Rust和Go使自己多样化,我变得过于担心复制/引用/移动等。

最近,我真的很想知道ValueTuple是否也受到struct的典型警告,即其大小不应大于16字节,以避免在复制值类型时出现性能问题:https://stackoverflow.com/a/1082341/4636721

那么,如果我们有一个值元组(decimal, decimal, decimal, decimal),这意味着我们最好使用经典的Tuple<decimal, decimal, decimal, decimal>类来传递该元组?

[编辑]

用例的一个例子:假设下面的方法是调用很多

public (decimal, decimal, decimal, decimal) GetSuperImportantTuple(int input)
{
var aParameter = GetAParameter(input);
// Copy when getting that tuple
var tuple = GetA4DecimalsValueTuple();
// Copy into that function
var anotherParameter = GetAnotherParameter(tuple);
// Copy when returning the value
return TransformValueTuple(tuple, anotherParameter);
}

就像往常一样,这取决于情况。值类型和引用类型不同。这种差异可能与性能有关。但是,您也可以决定只传递引用参数。如果你想知道在这种情况下什么真正跑得更快,以及你想要使用它的方式,可以用秒表测试它。绩效,通常是衡量的东西,而不是要求的东西。

相关内容

最新更新