如何确定 C# 中迭代或递归更快?

问题描述 投票:0回答:1

我试图确定 C# 中迭代或递归更快。然而,在一个简单的测试中,我似乎无法使用 StopWatch 获得可靠的结果。

代码:

internal class Program
    {
        // The factorial of a number is the product of all the
        // integers from 1 to that number.
        // For example, the factorial of 6 is 1*2*3*4*5*6 = 720
        // Factorial is not defined for negative numbers, and the factorial of zero is one, 0! = 1 
        static void Main(string[] args)
        {
            Console.WriteLine("Please Enter a Number:");

            //read number from user
            int number = Convert.ToInt32(Console.ReadLine());
            
            double sw1Elapsed = 0d;
            double sw2Elapsed = 0d;
            double factorial = 0d;
            double factorial2 = 0d;

            Stopwatch sw = new Stopwatch();
            sw.Reset(); 
            for (int i = 0; i < 6; i++)
            {
                sw.Restart();
                factorial = Factorial(number);
                sw.Stop();
                sw1Elapsed = sw1Elapsed + sw.Elapsed.TotalMilliseconds;

                sw.Restart();
                factorial2 = FactorialByRecursion(number);
                sw.Stop();
                sw2Elapsed = sw2Elapsed + sw.Elapsed.TotalMilliseconds;

                //print the factorial result
                Console.WriteLine("Iteration:");
                Console.WriteLine("Factorial of " + number + " = " + factorial.ToString());
                Console.WriteLine("Iteration Time taken = " + sw1Elapsed);
                Console.WriteLine();
                Console.WriteLine("Recursion:");
                Console.WriteLine("Factorial of " + number + " = " + factorial2.ToString());
                Console.WriteLine("Recursion Time taken = " + sw2Elapsed);
                Console.WriteLine("-----------------------------------------------------\n");
            }
        }


        public static double Factorial(int number)
        {
            if (number == 0)
                return 1;

            double factorial = 1;
            
            for (int i = number; i >= 1; i--)
            {
                factorial = factorial * i;
            }
            return factorial;
        }

        public static double FactorialByRecursion(int number)
        {
            if (number == 0)
                return 1;
            return number * FactorialByRecursion(number - 1);//Recursive call

        }
    }
}

所以有两个函数以两种不同的方式做同样的事情。

这些产生了这些结果:


Please Enter a Number: 6 Iteration: Factorial of 6 = 720 Iteration Time taken = 0.3171
Recursion: Factorial of 6 = 720 Recursion Time taken = 0.0616
-----------------------------------------------------
Iteration: Factorial of 6 = 720 Iteration Time taken = 0.3172
Recursion: Factorial of 6 = 720 Recursion Time taken =
0.061700000000000005
-----------------------------------------------------
Iteration: Factorial of 6 = 720 Iteration Time taken =
0.31739999999999996
Recursion: Factorial of 6 = 720 Recursion Time taken =
0.06180000000000001
-----------------------------------------------------
Iteration: Factorial of 6 = 720 Iteration Time taken =
0.31749999999999995
Recursion: Factorial of 6 = 720 Recursion Time taken =
0.06190000000000001
-----------------------------------------------------
Iteration: Factorial of 6 = 720 Iteration Time taken =
0.31759999999999994
Recursion: Factorial of 6 = 720 Recursion Time taken =
0.06200000000000001
-----------------------------------------------------
Iteration: Factorial of 6 = 720 Iteration Time taken =
0.3177999999999999
Recursion: Factorial of 6 = 720 Recursion Time taken =
0.062100000000000016
-----------------------------------------------------
C:\Users\Dev\source\repos\SimpleRecursion\SimpleRecursion\bin\Debug\net6.0\SimpleRecursion.exe (process 26992) exited with code 0. To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops. Press any key to close this window . . .

但是,如果我将第二个函数移动到循环中第一个函数的上方,那么可能是:

static void Main(string[] args)
        {
            Console.WriteLine("Please Enter a Number:");

            //read number from user
            int number = Convert.ToInt32(Console.ReadLine());
            
            double sw1Elapsed = 0d;
            double sw2Elapsed = 0d;
            double factorial = 0d;
            double factorial2 = 0d;

            Stopwatch sw = new Stopwatch();
            sw.Reset(); 
            for (int i = 0; i < 6; i++)
            {
                sw.Restart();
                factorial2 = FactorialByRecursion(number);
                sw.Stop();
                sw2Elapsed = sw2Elapsed + sw.Elapsed.TotalMilliseconds;

                sw.Restart();
                factorial = Factorial(number);
                sw.Stop();
                sw1Elapsed = sw1Elapsed + sw.Elapsed.TotalMilliseconds;

                //print the factorial result
                Console.WriteLine("Iteration:");
                Console.WriteLine("Factorial of " + number + " = " + factorial.ToString());
                Console.WriteLine("Iteration Time taken = " + sw1Elapsed);
                Console.WriteLine();
                Console.WriteLine("Recursion:");
                Console.WriteLine("Factorial of " + number + " = " + factorial2.ToString());
                Console.WriteLine("Recursion Time taken = " + sw2Elapsed);
                Console.WriteLine("-----------------------------------------------------\n");
            }
        }

那么结果就变成:

Please Enter a Number:
6
Iteration:
Factorial of 6 = 720
Iteration Time taken = 0.0571

Recursion:
Factorial of 6 = 720
Recursion Time taken = 0.1066
-----------------------------------------------------

Iteration:
Factorial of 6 = 720
Iteration Time taken = 0.0571

Recursion:
Factorial of 6 = 720
Recursion Time taken = 0.1069
-----------------------------------------------------

Iteration:
Factorial of 6 = 720
Iteration Time taken = 0.0572

Recursion:
Factorial of 6 = 720
Recursion Time taken = 0.1071
-----------------------------------------------------

Iteration:
Factorial of 6 = 720
Iteration Time taken = 0.0572

Recursion:
Factorial of 6 = 720
Recursion Time taken = 0.1073
-----------------------------------------------------

Iteration:
Factorial of 6 = 720
Iteration Time taken = 0.0572

Recursion:
Factorial of 6 = 720
Recursion Time taken = 0.10750000000000001
-----------------------------------------------------

Iteration:
Factorial of 6 = 720
Iteration Time taken = 0.057300000000000004

Recursion:
Factorial of 6 = 720
Recursion Time taken = 0.10770000000000002
-----------------------------------------------------


C:\Users\Dev\source\repos\SimpleRecursion\SimpleRecursion\bin\Debug\net6.0\SimpleRecursion.exe (process 26100) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . . 

谁能解释一下经过时间的差异以及为什么/发生了什么?

c# recursion iteration stopwatch
1个回答
0
投票

评论中已经提到了,但是使用基准测试工具来处理这样的事情;例如https://benchmarkdotnet.org/

© www.soinside.com 2019 - 2024. All rights reserved.