c# - 函数数组与开关的性能

标签 c# switch-statement function-pointers

读完本主题后:Performance of array of functions over if and switch statementshttp://en.wikipedia.org/wiki/Branch_table ,我编写了一个小测试来衡量 switch/case 风格编码与函数数组之间的性能差异。函数调用(F 类成员)有意仅使用 cpu 容量(算术内容):没有系统调用、没有像控制台输出这样的 I/O 等。

最后,这两种方法之间的差异是 switch 方法快了 30% 左右!好吧,函数指针比 switch/case 慢一点。

所以我的问题是:我的测试对您来说有效吗?或者我是否引入了任何偏见导致了这些令人难以置信的结果? 30%!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {

//使用的仿函数:

        delegate void functor(int i, int j);

//switch 中使用的枚举:

        enum indexes
        {
            a = 0, b = 1, c = 2, d = 3, e = 4, f = 5,
            g = 6, h = 7, i = 8, j = 9, k = 10,
            l = 11, m = 12, n = 13, o = 14, p = 15,
            q = 16
        };

//具有不同可能调用的类:

        class F
        {
            long m_j = 1;
            public void A(int i, int j) { m_j = (i + j - 2) % (j / 3 + 1); }
            public void B(int i, int j) { m_j = (i + j - 3) % (j / 3 + 1); }
            public void C(int i, int j) { m_j = (i + j - 4) % (j / 3 + 1); }
            public void D(int i, int j) { m_j = (i + j - 5) % (j / 3 + 1); }
            public void E(int i, int j) { m_j = (i + j - 6) % (j / 3 + 1); }
            public void FF(int i, int j) { m_j = (i + j - 7) % (j / 3 + 1); }
            public void G(int i, int j) { m_j = (i + j - 8) % (j / 3 + 1); }
            public void H(int i, int j) { m_j = (i + j - 9) % (j / 3 + 1); }
            public void I(int i, int j) { m_j = (i + j - 10) % (j / 3 + 1); }
            public void J(int i, int j) { m_j = (i + j - 11) % (j / 3 + 1); }
            public void K(int i, int j) { m_j = (i + j - 12) % (j / 3 + 1); }
            public void L(int i, int j) { m_j = (i + j - 13) % (j / 3 + 1); }
            public void M(int i, int j) { m_j = (i + j - 14) % (j / 3 + 1); }
            public void N(int i, int j) { m_j = (i + j - 15) % (j / 3 + 1); }
            public void O(int i, int j) { m_j = (i + j - 16) % (j / 3 + 1); }
            public void P(int i, int j) { m_j = (i + j - 17) % (j / 3 + 1); }
            public void Q(int i, int j) { m_j = (i + j - 18) % (j / 3 + 1); }
            public static int nbfunc = 17;
        }


        static void Main(string[] args)
        {

//在每一轮中,我们都会增加调用次数:

            long maxi = 1000;
            for (; maxi < 10000000000; maxi *= 10)
            {
                long switch_time, array_time;
                TextWriter tw = Console.Out;
                {
                    Stopwatch sw = new Stopwatch();
                    F f = new F();

//************** 使用开关/外壳进行测试 **************

                    sw.Start();
                    for (int i = 0; i < maxi; i++)
                    {
                        indexes e = (indexes)(i % F.nbfunc);
                        switch (e)
                        {
                            case indexes.a:
                                f.A(i,i/2);
                                break;
                            case indexes.b:
                                f.B(i, i / 2);
                                break;
                            case indexes.c:
                                f.C(i, i / 2);
                                break;
                            case indexes.d:
                                f.D(i, i / 2);
                                break;
                            case indexes.e:
                                f.E(i, i / 2);
                                break;
                            case indexes.f:
                                f.FF(i, i / 2);
                                break;
                            case indexes.g:
                                f.G(i, i / 2);
                                break;
                            case indexes.h:
                                f.H(i, i / 2);
                                break;
                            case indexes.i:
                                f.I(i, i / 2);
                                break;
                            case indexes.j:
                                f.J(i, i / 2);
                                break;
                            case indexes.k:
                                f.K(i, i / 2);
                                break;
                            case indexes.l:
                                f.L(i, i / 2);
                                break;
                            case indexes.m:
                                f.M(i, i / 2);
                                break;
                            case indexes.n:
                                f.N(i, i / 2);
                                break;
                            case indexes.o:
                                f.O(i, i / 2);
                                break;
                            case indexes.p:
                                f.P(i, i / 2);
                                break;
                        }
                    }
                    sw.Stop();
                    switch_time = sw.ElapsedMilliseconds;
                }
                //

//************** 使用函数数组进行测试 **************

                {

                    Stopwatch sw = new Stopwatch();
                    F f = new F();

                    List<functor> functors = new List<functor>()
                    {
                        f.A, f.B, f.C, f.D, f.E, f.FF, f.G, f.H, f.I, f.J, f.K, f.L, f.M, f.N, f.O, f.P, f.Q 
                    };

                    sw.Start();
                    for (int i = 0; i < maxi; i++)
                    {
                        functors[i % F.nbfunc](i, i / 2);
                    }
                    sw.Stop();
                    array_time = sw.ElapsedMilliseconds;
                }

//************* 显示结果 *************

                Console.WriteLine("nb iterations : " + maxi.ToString());
                Console.WriteLine("  switch method total time in ms : " + (switch_time).ToString());
                Console.WriteLine("  array  method total time in ms : " + (array_time).ToString());


            }

        }

    }
}

在 win7 64 位、VS2010、Xeon E5-2609 @ 2.4 Ghz 上编译 编译器标志:视觉标准模式发布,带有“优化代码”标志。

结果:

nb iterations : 1000000
  switch method total time in ms : 19
  array  method total time in ms : 23
nb iterations : 10000000
  switch method total time in ms : 177
  array  method total time in ms : 237
nb iterations : 100000000
  switch method total time in ms : 1808
  array  method total time in ms : 2416
nb iterations : 1000000000
  switch method total time in ms : 18630
  array  method total time in ms : 24312

最佳答案

唯一值得注意的是,您正在为每次 maxi 迭代新建仿函数列表。如果将其移到循环之外,您的计时会是什么样子?

关于c# - 函数数组与开关的性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23913050/

相关文章:

c# - 等效于内联输出参数声明?

c# - 静态场的替代品

php - 切换大小写循环 PHP

c - 为什么函数指针可以带或不带运算符地址使用?

c# - RESTful WCF 服务在发送 "raw"XML 时返回 400 代码

c# - 将货币从 3 位数转换为 ISO 4217

java - 如何a=3和b=4?

android - 动态分配资源与大开关案例

c++ - 使用指向独立函数的指针调用 std::thread

c - 如何在 C 中打印函数指针的值?