简体   繁体   English

Array.sort(或任何集合的排序方法)是否取决于C#中单个项目的大小?

[英]does Array.sort (or any collections sort method) depends on size of an individual items in C#?

As we all know the sort method the array works upon. 众所周知,数组适用于sort方法。 The speed is affected by the number of items stored in an array. 速度受阵列中存储的项目数影响。 However would the speed be affected by the size of the individual items? 但是,速度会受单个项目的大小影响吗? I searched this over the net but i haven't found a convincing answer. 我在网上搜索了此信息,但没有找到令人信服的答案。 Anybody got helpful links or explanation that can be displayed here? 有人获得了可以在此处显示的有用链接或说明吗?

For example we can have an array storing 100 string items which are all 20 string characters in length. 例如,我们可以有一个数组,其中存储了100个字符串项目,这些字符串项目的长度均为20个字符串字符。 Would the speed of sorting differ with 100 string items which are all 30 string characters? 100个都是30个字符串字符的字符串项目的排序速度是否会有所不同?

Thank you 谢谢

As long as it's reference types, no, the size will not matter. 只要是引用类型,不,大小无关紧要。 A reference is always the same size, no matter how large the object is that it points to, so swapping two refererences is constant time no matter what they point to. 无论引用指向的对象有多大,引用的大小总是相同的,因此交换两个引用是恒定的时间,无论它们指向什么。

Edit: Now that you refined the question, obviously figuring out the sorting order will take more time, the more complex comparing two objects is. 编辑:现在,您已经对问题进行了细化,显然弄清楚排序顺序将花费更多的时间,而比较两个对象则更加复杂。 If you have 100 strings of 100 characters each that differ in the first two characters, sorting them will be faster than having 100 strings of 100 characters that all contain 98 'a's and only differ in the last two. 如果您有100个字符串,每个字符串包含100个字符,且前两个字符不同,则对它们进行排序的速度将比具有100个字符的100个字符串(都包含98个'a',并且仅在后两个字符中有所不同)快。

Generally speaking, before you wonder about performance, make sure you actually have a problem. 一般来说,在对性能感到疑惑之前,请确保您确实有问题。 Comparing 100 strings is peanuts for todays hardware. 比较100串字符串对于当今的硬件来说是花生。

No. The size of an object Under the covers the sort method moves pointers in a collection, and pointers are always the same size regardless of the structure that they point at. 否。对象的大小在幕后,sort方法将指针移动到集合中,并且指针的大小始终相同,无论它们指向的结构如何。 In addition to collection size the sort method time is affected by the amount of time that it takes to compare two objects in the list. 除了集合大小之外,排序方法时间还受比较列表中两个对象所花费的时间量的影响。

Would the speed of sorting differ with 100 string items which are all 30 string characters? 100个都是30个字符串字符的字符串项目的排序速度是否会有所不同?

Potentially, yes. 可能是的。 Potentially, no. 可能不会。 It depends. 这取决于。 If you have a bunch of strings which only differ at character 30, that will take longer to sort than a bunch of strings which only differ at character 20 - simply because each string comparison will have more work to do. 如果您有一串在字符30处不同的字符串,则排序所花的时间将比一串在20个字符处不同的字符串要花费更长的时间-仅仅是因为每个字符串比较都需要做更多的工作。 On the other hand, if you have a bunch of strings which differ in their first characters, then the comparisons will be quick no matter how many trailing characters there are. 另一方面,如果您有一堆字符串的第一个字符不同,那么不管有多少个尾随字符,比较都会很快。

Also note that the more data you're using, the less effective the CPU cache may be, which can make a huge difference. 另请注意,您使用的数据越多,CPU缓存的效率就越低,这可能会产生很大的不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM