推荐High Performance C# NuGet 包

High Performance C# 通用算法的 NuGet 包。在 Windows 和 Linux(.NET 5 和 6、.NET Standard 2.0 和 2.1)上运行。社区驱动以提高 C# 性能。熟悉的接口,类似于标准 C# 算法和 Linq,在nuget.org 上的免费、开源。
在这里插入图片描述
GitHub:https://github.com/DragonSpit/HPCsharp

示例代码

using HPCsharp.Algorithms;
using HPCsharp.ParallelAlgorithms;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace HPCsharpExamples
{
    partial class Program
    {
        public static void SumMeasureArraySpeedup()
        {
            Random randNum = new Random(2);
            int arraySize = 64 * 1024 * 1024;
            int[] benchArrayOne = new int[arraySize];

            for (int i = 0; i < arraySize; i++)
            {
                benchArrayOne[i] = randNum.Next(0, 64);    // limit the range to not cause overflow exception for standard C# .Sum()
            }

            Stopwatch stopwatch = new Stopwatch();
            long frequency = Stopwatch.Frequency;
            long nanosecPerTick = (1000L * 1000L * 1000L) / frequency;

            stopwatch.Restart();
            int sum = benchArrayOne.Sum();
            stopwatch.Stop();
            double timeForSum = stopwatch.ElapsedTicks * nanosecPerTick / 1000000000.0;

            stopwatch.Restart();
            int sumAsParallel = benchArrayOne.AsParallel().Sum();
            stopwatch.Stop();
            double timeForSumAsParallel = stopwatch.ElapsedTicks * nanosecPerTick / 1000000000.0;

            stopwatch.Restart();
            long sumHpc = benchArrayOne.SumToLong();
            stopwatch.Stop();
            double timeForSumHpc = stopwatch.ElapsedTicks * nanosecPerTick / 1000000000.0;

            stopwatch.Restart();
            long sumSse = benchArrayOne.SumToLongSse();
            stopwatch.Stop();
            double timeForSumSse = stopwatch.ElapsedTicks * nanosecPerTick / 1000000000.0;


            stopwatch.Restart();
            long sumSsePar = benchArrayOne.SumToLongSsePar();
            stopwatch.Stop();
            double timeForSumSsePar = stopwatch.ElapsedTicks * nanosecPerTick / 1000000000.0;

            //Console.WriteLine("C# array of size {0}: Sum   {1:0.000} sec", arraySize, timeForSum);
            Console.WriteLine("C# array of size {0}: Sum              {1:0.000} sec, HPC#.SumToLong       {2:0.000} sec, speedup {3:0.00}", arraySize,
                               timeForSum, timeForSumHpc, timeForSum / timeForSumHpc);
            Console.WriteLine("C# array of size {0}: Sum              {1:0.000} sec, HPC#.SumToLongSse    {2:0.000} sec, speedup {3:0.00}", arraySize,
                               timeForSum, timeForSumSse, timeForSum / timeForSumSse);
            Console.WriteLine("C# array of size {0}: Sum.AsParallel   {1:0.000} sec, HPC#.SumToLongSse    {2:0.000} sec, speedup {3:0.00}", arraySize,
                               timeForSumAsParallel, timeForSumSse, timeForSumAsParallel / timeForSumSse);
            Console.WriteLine("C# array of size {0}: Sum.AsParallel   {1:0.000} sec, HPC#.SumToLongSsePar {2:0.000} sec, speedup {3:0.00}", arraySize,
                               timeForSumAsParallel, timeForSumSsePar, timeForSumAsParallel / timeForSumSsePar);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

andy421

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值