Which of the following is faster to traverse a single-chain table and list?

Source: Internet
Author: User

): "C # The event is backed by a delegated linked List (single-chain table). The traversal and calling performance of a single-chain table is much lower than that of an array linked List (List <T> )". I am puzzled by this sentence, because from my intuition, even if there is a performance gap between the two methods, it should not be "much higher. But after I raised this question, firelong responded (or the original saying) "indirect pointer movement, which speed is difficult to identify with I ++ ?" So I thought, let's do a test. The trial code is simple:

public class Node{    public Node Next;    public int Value;}public class Item{    public int Value;}class Program{    static Node GetSingleList(int length)    {        Node root = null;        for (int i = 0; i < length; i++)        {            root = new Node { Next = root, Value = 0 };        }        return root;    }    static List<Item> GetList(int length)    {        return Enumerable.Range(0, length)            .Select(_ => new Item { Value = 0 }).ToList();    }    static void Main(string[] args)    {        int length = 10000;        int iteration = 100000;        int count = 0;        var root = GetSingleList(length);        var watch1 = Stopwatch.StartNew();        for (int t = 0; t < iteration; t++)        {            var node = root;            while (node != null)            {                count += node.Value;                node = node.Next;            }        }        Console.WriteLine("{0} (Node List)", watch1.Elapsed);        GC.Collect();        var list = GetList(length);        var watch2 = Stopwatch.StartNew();        for (int t = 0; t < iteration; t++)        {            for (int i = 0; i < list.Count; i++)            {                count += list[i].Value;            }        }        Console.WriteLine("{0} (List<Item>)", watch2.Elapsed);    }}

Run the following command to compile in Release mode and ensure that VS does not Attach the Debugger:

00:00:02.0731861 (Node List)00:00:02.4602990 (List<Item>)00:00:02.3176291 (Node List)00:00:02.2912638 (List<Item>)00:00:02.1539642 (Node List)00:00:02.4635390 (List<Item>)

 

My intuition is as follows: if you use List <T> to traverse, in addition to the I ++ operation, you also need to calculate the offset and find the address of the next object based on the array in the List, then, access the next object based on this address, and traverse the one-way linked list will do less, as long as one access is needed. From the results, the overall difference is not big, and there is no "one-way linked List traversal performance is much lower than List <T>" mentioned by firelong. In fact, does this performance really matter? A total of 1 billion elements are traversed here, which leads to a gap of several seconds at. How many Handler will you add to an event and how many calls will you call?

I have been reluctant to talk about performance issues because I have nothing to talk about. In addition, I have never suffered any pains in this regard. Even if I encounter some small problems, it is also because the code writing efficiency is not high, and there will be no problem after simple optimization. However, firelong's new article talks about design, for example, C. NET's event mechanism is very bad, yield is useless, and C # syntax is very bloated. I like languages. I like language design very much, so I can discuss these aspects. I will write more recently. You can follow my new blog.

This article was written in a hurry, and there was nothing to take. Let's just give it a try.

Three additional points:

A friend suggested that the distribution of objects in the array in the memory is continuous and the one-way linked list is not continuous. Therefore, considering the relationship between page feed and cache, the array-based efficiency will be relatively high. My opinion is: If you traverse int [], then the memory of each int value is naturally continuous, but here we access the array of referenced elements such as item []. The continuous distribution is only the address of the object. To obtain the final object, we have to access a memory according to the address, it cannot guarantee continuity. In the same sense, some friends said that in actual circumstances, objects such as node are not continuously accessed, and I don't think this difference will be biased towards either of them. Some friends think that the address in the array is continuous, and the locality is better. However, in my opinion, for a one-way linked list, for example, when accessing the value of node, next will also be loaded into the cache. The fields of the same object are closely related. net for local considerations.

Another friend pointed out that it would not be silly to access the subscript through array I ++ and it would be optimized. This is true. If you use item [] instead of list <item>, you will find that the performance is indeed improved (but the difference is not big ). But what firelong says is list <t>, which is not an array, but an array-based container. Since list <t> is variable, it is still unknown whether JIT will actually optimize it. I tend to understand it as "no ". But now, I just use a small experiment to see what the difference is.

Some friends also said that delegate may not be saved by a one-way linked list or list <t>. Yes, some friends have already put forward it in the original post of firelong. However, I only aim at firelong's comparison of the traversal performance between a single linked list and a list <t>. I 've been wondering, from the previous article, firelong said, "The performance is very different." It's far lower than ", and it's a very serious term like" Fatal Impact, however, to what extent it is really serious, there is no argument at all. So now I am just starting to explain some very serious things of firelong. You have to make your own research.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.