zoukankan      html  css  js  c++  java
  • C# Method Call Depth Performance

    C# Method Call Depth Performance

    Compare the performance of method call depths. Reducing call depth can improve speed.

    Method calls can be nested. This impacts performance. The more nesting, the more depth to the stack. A method call will require stack space unless it is inlined. By flattening the call stack, we reduce stack usage and improve performance.

    Example. In compiler theory, the term "activation record" refers to a section of memory on the stack that contains data about a method call. As more nested methods are invoked, the stack grows downwards, consuming more and more memory.

    And: When a method terminates, its memory is forgotten from the stack and the pointer is readjusted.

    Next: This program contains eight important methods: A1 through D1, and A2 through D2.

    Info: When A1 is called, it calls B1 and then B1 calls C1. On the other hand, when A2 is called, it simply calls B2, C2, and D2.

    Important: The A2 method should consume less stack space. It has a shorter stack.

    using System;
    using System.Diagnostics;
    
    class Program
    {
        const int _max = 100000000;
        static void Main()
        {
            A1();
            A2();
    
            var s1 = Stopwatch.StartNew();
            for (int i = 0; i < _max; i++)
            {
                A1();
            }
            s1.Stop();
            var s2 = Stopwatch.StartNew();
            for (int i = 0; i < _max; i++)
            {
                A2();
            }
            s2.Stop();
            Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000 * 1000) /
                _max).ToString("0.00 ns"));
            Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000 * 1000) /
                _max).ToString("0.00 ns"));
            Console.Read();
        }
    
        static int _i = 1;
    
        static void A1()
        {
            B1();
        }
    
        static void B1()
        {
            if (_i == 0)
                throw new Exception();
            C1();
        }
    
        static void C1()
        {
            if (_i == 0)
                throw new Exception();
            D1();
        }
    
        static void D1()
        {
            if (_i == 0)
                throw new Exception();
        }
    
        static void A2()
        {
            B2();
            C2();
            D2();
        }
    
        static void B2()
        {
            if (_i == 0)
                throw new Exception();
        }
    
        static void C2()
        {
            if (_i == 0)
                throw new Exception();
        }
    
        static void D2()
        {
            if (_i == 0)
                throw new Exception();
        }
    }

    Output

    3.54 ns Nested calls

    0.96 ns Sequential calls

    The results of this program are revealing. When A1, B1, C1 and D1 are called in a nested sequence, the program is much slower than when A2, B2, C2 and D2 are called in a more sequential order.

    Also: The Exception throws in the program are to confuse the inliner in the JIT.

    Summary. It is possible to improve the performance of method calls by reducing the depth of the call stack. In other words, changing deeply nested method invocations into more sequential invocations can yield performance benefits.

    However: This is likely not useful except on performance-critical parts of important programs.

  • 相关阅读:
    败家MM
    Lucene.Net
    罗氏制药事件
    关于request编码的问题
    昨天做了个站内全文搜索
    有时间研究 ror框架,ProMesh.NET: 开源的.net MVC框架
    DotLucene搜索引擎Demo之:创建索引
    超低密度SiO2气凝胶的制备及成型研究
    使用System.out.print/prilntln() 输出时存在的问题
    对C++对象实例化的测试
  • 原文地址:https://www.cnblogs.com/chucklu/p/12671821.html
Copyright © 2011-2022 走看看