Math optimization in C#

后端 未结 25 2407
悲&欢浪女
悲&欢浪女 2020-12-07 10:25

I\'ve been profiling an application all day long and, having optimized a couple bits of code, I\'m left with this on my todo list. It\'s the activation function for a neural

25条回答
  •  广开言路
    2020-12-07 11:04

    FWIW, here's my C# benchmarks for the answers already posted. (Empty is a function that just returns 0, to measure the function call overhead)

    Empty Function:       79ms   0
    Original:             1576ms 0.7202294
    Simplified: (soprano) 681ms  0.7202294
    Approximate: (Neil)   441ms  0.7198783
    Bit Manip: (martinus) 836ms  0.72318
    Taylor: (Rex Logan)   261ms  0.7202305
    Lookup: (Henrik)      182ms  0.7204863
    
    public static object[] Time(Func f) {
        var testvalue = 0.9456;
        var sw = new Stopwatch();
        sw.Start();
        for (int i = 0; i < 1e7; i++)
            f(testvalue);
        return new object[] { sw.ElapsedMilliseconds, f(testvalue) };
    }
    public static void Main(string[] args) {
        Console.WriteLine("Empty:       {0,10}ms {1}", Time(Empty));
        Console.WriteLine("Original:    {0,10}ms {1}", Time(Original));
        Console.WriteLine("Simplified:  {0,10}ms {1}", Time(Simplified));
        Console.WriteLine("Approximate: {0,10}ms {1}", Time(ExpApproximation));
        Console.WriteLine("Bit Manip:   {0,10}ms {1}", Time(BitBashing));
        Console.WriteLine("Taylor:      {0,10}ms {1}", Time(TaylorExpansion));
        Console.WriteLine("Lookup:      {0,10}ms {1}", Time(LUT));
    }
    

提交回复
热议问题