.net 什么时候使用元组和 KeyValuePair 更好?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19522422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:31:38  来源:igfitidea点击:

When is it better to use a Tuple versus a KeyValuePair?

.netsemantics

提问by Nick Gotch

I've generally used the KeyValuePair<TKey,TValue>type whenever I have data that is pair-related in the sense that one is a key to the other. If the data is unrelated then the Tuple<T1,T2>type makes more sense and I'd go with that.

KeyValuePair<TKey,TValue>每当我有成对相关的数据时,我通常都会使用该类型,因为一个是另一个的关键。如果数据不相关,那么Tuple<T1,T2>类型更有意义,我会这样做。

Now I just read this articleabout why to generally avoid KeyValuePair<TKey,TValue>and prefer Tuple<T1,T2>. The primary argument being the performance benefit of Tuple<T1,T2>.

现在我刚刚阅读了这篇关于为什么要普遍避免KeyValuePair<TKey,TValue>和喜欢Tuple<T1,T2>. 主要论点是Tuple<T1,T2>.

Outside performance, is there any reason a KVP would be a better choice than a Tuple<T1,T2>?

外部性能,是否有任何理由 KVP 会比 a 更好的选择Tuple<T1,T2>

采纳答案by vcsjones

Well, the type could be considered poorly named, for one. A KeyValuePair as named should represent a key and a value. What if your two objects aren't really a key and a value, just two things? If I were to see a method or property that had a type of KeyValuePair<TKey, TValue>, I would expect the values of the KVP to be a key and a value. This is really just a matter of communicating intention and making it clear to yourself in the future, or possibly other team members. A tuple does not indicate that kind of association.

嗯,该类型可以被认为是不好命名的,例如。命名的 KeyValuePair 应该表示一个键和一个值。如果你的两个对象不是真正的键和值,只有两件事怎么办?如果我看到一个方法或属性的类型为KeyValuePair<TKey, TValue>,我希望 KVP 的值是一个键和一个值。这实际上只是传达意图并在未来向自己或其他团队成员明确表达的问题。元组并不表示那种关联。

Tuples also make it easier to add another value, making it a 3-tuple (or a triplet, however you want to call it). Some .NET languages, like F#, have special syntaxaround tuples as well.

元组还可以更轻松地添加另一个值,使其成为 3 元组(或三元组,无论您想如何称呼它)。一些 .NET 语言,如 F#,也有围绕元组的特殊语法

For an implementation perspective, Tupledoes many things KeyValuePairdoes not. Tuples are comparable, they implement the IComparableand IStructuralEquatableinterfaces, so it makes it easier to compare two tuples.

从实现的角度来看,Tuple做很多事情KeyValuePair没有。元组具有可比性,它们实现了IComparableIStructuralEquatable接口,因此可以更轻松地比较两个元组。

回答by Sriram Sakthivel

KeyValuePairis struct and Tupleis a class.

KeyValuePair是 struct 并且Tuple是一个类。

That is the main difference which influences how the objects are copied whether by reference or values.

这是影响对象如何通过引用或值复制的主要区别。

and hence Tuple<T1,T2>when passed around just uses "4byte" in 32bit OS, whereas KeyValuePair<K,V>requires more based on "K and V"

因此Tuple<T1,T2>,在 32 位操作系统中传递时仅使用“4byte”,而KeyValuePair<K,V>需要更多基于“K 和 V”

Anyhow comparing Tuple and KeyValuePair is not a good idea(doesn't makes sense for me) since both serves different purpose.

无论如何,比较 Tuple 和 KeyValuePair 不是一个好主意(对我来说没有意义),因为两者都有不同的用途。

回答by Special Sauce

Despite the semantics, performance may be an important consideration as you consider both options. As previously mentioned, the KeyValuePairis a value type (struct), whereas the Tuple<>is a reference type (class). Therefore, the KeyValuePairis allocated on the stack and the Tuple<>is allocated on the heap, and the optimal choice is usually determined by the classic arguments of Stack vs. Heap Memory Allocation. In short, stack space is limited, but generally has very fast access. The heap memory is much larger but is somewhat slower.

尽管存在语义,但在您考虑这两个选项时,性能可能是一个重要的考虑因素。如前所述,KeyValuePair是值类型(结构),而Tuple<>是引用类型(类)。因此,在KeyValuePair堆栈Tuple<>上分配和在堆上分配,最佳选择通常由Stack vs. Heap Memory Allocation的经典参数决定。简而言之,堆栈空间是有限的,但通常访问速度非常快。堆内存要大得多,但速度稍慢。

KeyValuePair<T1, T2>may be the better choice if both the key and value types are primitives (value types like int, bool, double, etc.) or structs of small size. With primitive types on the stack, allocation and deallocation is lightning fast. This can really affect performance, especially as arguments to recursive method calls.

KeyValuePair<T1, T2>可能是更好的选择,如果这两个键和值的类型是基元(值类型喜欢intbooldouble等),或小尺寸的结构。使用堆栈上的原始类型,分配和释放速度快如闪电。这确实会影响性能,尤其是作为递归方法调用的参数。

On the other hand, Tuple<T1, T2>is likely the better choice if either T1or T2are reference types (like classes). A KeyValuePairthat contains pointers to reference types (as the key or value types) sort of defeats the purpose since the objects will need to be looked up on the heap anyway.

另一方面,Tuple<T1, T2>如果T1T2是引用类型(如类),则可能是更好的选择。KeyValuePair包含指向引用类型(作为键或值类型)的指针的A有点违背目的,因为无论如何都需要在堆上查找对象。

Here's a benchmark I found online: Tuple vs. KeyValuePair. The only problem with this benchmark is that they tested KeyValuePair<string, string>vs. Tuple<string, string>, and the stringtype is an unusual and special type in .NET in that it can behave both like a value type and/or a reference type depending on the execution context. I believe the KeyValuePair<int, int>would have been a clear winner against Tuple<int, int>. Even with the deficiencies, however, the results show that the performance differences can be significant:

这是我在网上找到的一个基准:Tuple vs. KeyValuePair。与此基准唯一的问题是,它们测试KeyValuePair<string, string>Tuple<string, string>string类型是.NET一个不寻常的和特殊类型的,它可以表现两者的值等的类型和/或根据所述执行上下文的引用类型。我相信KeyValuePair<int, int>会是一个明显的赢家Tuple<int, int>。然而,即使存在缺陷,结果也表明性能差异可能很大:

8.23 ns -- Allocate Tuple
0.32 ns-- Allocate KeyValuePair (25x faster!)

1.93 ns -- Pass Tuple as argument
2.57 ns -- Pass KeyValuePair as argument

1.91 ns -- Return Tuple
6.09 ns -- Return KeyValuePair

2.79 ns -- Load Tuple from List
4.18 ns -- Load KeyValuePair from List

8.23 ns -- 分配元组
0.32 ns-- 分配 KeyValuePair (快 25 倍!)

1.93 ns -- 将元组作为参数
传递 2.57 ns -- 将 KeyValuePair 作为参数传递

1.91 ns -- 返回元组
6.09 ns -- 返回 KeyValuePair

2.79 ns -- 从列表加载元组
4.18 ns -- 从列表加载 KeyValuePair

回答by MikeT

Your really asking the wrong question the proper question is using a Class(Tuple)_ better than a Struct(KVP) in which case the is answer is what do you want to use them for and the answer is given here Structs versus classes

你真的问错了问题,正确的问题是使用类(元组)_比结构(KVP)更好,在这种情况下,答案是你想用它们做什么,答案在这里给出结构与类