using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Numerics;
namespace Sample3
{
class Program
{
static void Main(string[] args)
{
//准确获取运行时间
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("一般串行开始.....");
sw.Start();
Calc100();
Calc100();
Calc100();
sw.Stop();
Console.WriteLine("总耗时 = " + sw.ElapsedMilliseconds + "(ms)");
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("并行处理开始.....");
sw.Restart();
sw.Start();
Parallel.Invoke(Calc100, Calc100, Calc100);
sw.Stop();
Console.WriteLine("总耗时= " + sw.ElapsedMilliseconds + "(ms)");
Console.ReadLine();
}
static void Calc100()
{
int n = 1000000;
BigInteger b = new BigInteger(long.MaxValue);
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
for (var k = 1; k <= n; k++)
{
BigInteger b2 = k * b;
}
sw.Stop();
Console.WriteLine("calc " + n + " times bigint multiply cost time " + sw.ElapsedMilliseconds + "(ms)");
C#并行运算 Parallel.Invoke、Parallel.For、Parallel.Foreach性能测试及示例
最新推荐文章于 2024-04-27 03:45:00 发布