Ternary operator is twice as slow as an if-else block?

后端 未结 10 953
无人及你
无人及你 2020-11-30 16:20

I read everywhere that ternary operator is supposed to be faster than, or at least the same as, its equivalent if-else block.

However, I di

10条回答
  •  渐次进展
    2020-11-30 16:58

    I did what Jon Skeet did and ran through 1 iteration and 1,000 iterations and got a different result from both OP and Jon. In mine, the ternary is just slightly faster. Below is the exact code:

    static void runIfElse(int[] array, int iterations)
        {
            long value = 0;
            Stopwatch ifElse = new Stopwatch();
            ifElse.Start();
            for (int c = 0; c < iterations; c++)
            {
                foreach (int i in array)
                {
                    if (i > 0)
                    {
                        value += 2;
                    }
                    else
                    {
                        value += 3;
                    }
                }
            }
            ifElse.Stop();
            Console.WriteLine(String.Format("Elapsed time for If-Else: {0}", ifElse.Elapsed));
        }
    
        static void runTernary(int[] array, int iterations)
        {
            long value = 0;
            Stopwatch ternary = new Stopwatch();
            ternary.Start();
            for (int c = 0; c < iterations; c++)
            {
                foreach (int i in array)
                {
                    value += i > 0 ? 2 : 3;
                }
            }
            ternary.Stop();
    
    
            Console.WriteLine(String.Format("Elapsed time for Ternary: {0}", ternary.Elapsed));
        }
    
        static void Main(string[] args)
        {
            Random r = new Random();
            int[] array = new int[20000000];
            for (int i = 0; i < array.Length; i++)
            {
                array[i] = r.Next(int.MinValue, int.MaxValue);
            }
            Array.Sort(array);
    
            long value = 0;
    
            runIfElse(array, 1);
            runTernary(array, 1);
            runIfElse(array, 1000);
            runTernary(array, 1000);
            
            Console.ReadLine();
        }
    

    The output from my program:

    Elapsed time for If-Else: 00:00:00.0140543

    Elapsed time for Ternary: 00:00:00.0136723

    Elapsed time for If-Else: 00:00:14.0167870

    Elapsed time for Ternary: 00:00:13.9418520

    Another run in milliseconds:

    Elapsed time for If-Else: 20

    Elapsed time for Ternary: 19

    Elapsed time for If-Else: 13854

    Elapsed time for Ternary: 13610

    This is running in 64-bit XP, and I ran without debugging.

    Edit - Running in x86:

    There's a big difference using x86. This was done without debugging on and on the same xp 64-bit machine as before, but built for x86 CPUs. This looks more like OP's.

    Elapsed time for If-Else: 18

    Elapsed time for Ternary: 35

    Elapsed time for If-Else: 20512

    Elapsed time for Ternary: 32673

提交回复
热议问题