zl程序教程

您现在的位置是:首页 >  其他

当前栏目

go和c#实现斐波那契数列

2023-04-18 12:56:54 时间

首先通过C#实现斐波那契数列:

using System.Threading.Channels;

namespace App001
{
    internal class Program
    {

        static async Task Main()
        {
            var count = 45;
            await SomeTask(count);  //channel run time:00:00:10.0122552ms
            //await OneTask(count);   //run time:00:00:23.1586639ms         
            Console.Read();     //多次运行结果类似
        }


        static async Task SomeTask(int count)
        {
            var startTime = DateTime.Now;
            var channel = Channel.CreateUnbounded<long>();
            for (int i = 0; i < count; i++)
            {
                await channel.Writer.WriteAsync(i);
            }
            channel.Writer.Complete();



            List<Task> tasks = new List<Task>();
            for (int i = 0; i < 10; i++)
            {
                var task = Task.Factory.StartNew(async () =>
                {
                    while (await channel.Reader.WaitToReadAsync())
                    {
                        if (channel.Reader.TryRead(out var result))
                        {
                            /***/
                            Console.WriteLine(Fib(result));
                        }
                    }
                });
                tasks.Add(task);
            }

            await Task.WhenAll(tasks.ToArray()).ContinueWith(t =>
            {
                Console.WriteLine($"channel run time:{ DateTime.Now.Subtract(startTime)}ms");
            });
        }

        static Task OneTask(int count)
        {
            var startTime = DateTime.Now;
            for (int i = 0; i < count; i++)
            {
                Console.WriteLine(Fib(i));
            }
            Console.WriteLine($"run time:{ DateTime.Now.Subtract(startTime)}ms");
            return Task.CompletedTask;
        }

        static long Fib(long n)
        {
            if (n <= 2)
                return 1;
            else
                return Fib(n - 1) + Fib(n - 2);
        }
    }
}

这里是一个任务cpu和内存占用情况:

这里是十个任务cpu和内存占用情况:

结果:

 

下面是go实现斐波那契的代码:

func main() {
    startTime := time.Now()
    jobs := make(chan int, 100)
    results := make(chan int, 100)
    for count := 0; count < 10; count++ {
        go worker(jobs, results)
    }

    for i := 0; i < 45; i++ {
        jobs <- i
    }

    close(jobs)

    for j := 0; j < 45; j++ {
        fmt.Println(<-results)
    }
    endTime := time.Now()
    fmt.Println("channel run time:", endTime.Sub(startTime), "ms")
}

func worker(jobs <-chan int, results chan<- int) {
    for n := range jobs {
        results <- fib(n)
    }
}

func fib(n int) int {
    if n <= 2 {
        return 1
    }
    return fib(n-1) + fib(n-2)
}

cpu和内存占用情况:

运行结果:

 

代码示例:

exercise/斐波那契Test at master · liuzhixin405/exercise (github.com)

go/concurrencyTest at main · liuzhixin405/go (github.com)