C# 多核循环

使用Parallel.For可进行多核循环,加快循环速度。

实验:使用For和Parallel.For两种写法实现循环执行100次延时100毫秒。

实验结果:如果使用普通循环,需花费十秒;使用Parallel.For,需花费三秒(取决于CPU核心数量)。如循环执行顺序需升序,则不能使用Parallel.For。

普通循环代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    internal class Program
    {
        static void Main(string[] args)
        {
            DateTime start;
            DateTime end;
            TimeSpan expend;
            int n = 100; 

            start= DateTime.Now;
            for (int i = 0; i < n; i++)
            {
                Console.WriteLine($"Task {i} is running on thread {Task.CurrentId}");
                DoSomeWork(i);
            }
            end = DateTime.Now;
            expend = end - start;
            Console.WriteLine("All tasks completed.");
            Console.WriteLine(start.ToString("G"));
            Console.WriteLine(end.ToString("G"));
            Console.WriteLine(expend.ToString("G"));
            Console.Read();
        }

        static void DoSomeWork(int i)
        {
            Task.Delay(100).Wait();
        }
    }
}

普通循环输出:

Task 0 is running on thread
Task 1 is running on thread
Task 2 is running on thread
Task 3 is running on thread
Task 4 is running on thread
Task 5 is running on thread
Task 6 is running on thread
Task 7 is running on thread
Task 8 is running on thread
Task 9 is running on thread
Task 10 is running on thread
Task 11 is running on thread
Task 12 is running on thread
Task 13 is running on thread
Task 14 is running on thread
Task 15 is running on thread
Task 16 is running on thread
Task 17 is running on thread
Task 18 is running on thread
Task 19 is running on thread
Task 20 is running on thread
Task 21 is running on thread
Task 22 is running on thread
Task 23 is running on thread
Task 24 is running on thread
Task 25 is running on thread
Task 26 is running on thread
Task 27 is running on thread
Task 28 is running on thread
Task 29 is running on thread
Task 30 is running on thread
Task 31 is running on thread
Task 32 is running on thread
Task 33 is running on thread
Task 34 is running on thread
Task 35 is running on thread
Task 36 is running on thread
Task 37 is running on thread
Task 38 is running on thread
Task 39 is running on thread
Task 40 is running on thread
Task 41 is running on thread
Task 42 is running on thread
Task 43 is running on thread
Task 44 is running on thread
Task 45 is running on thread
Task 46 is running on thread
Task 47 is running on thread
Task 48 is running on thread
Task 49 is running on thread
Task 50 is running on thread
Task 51 is running on thread
Task 52 is running on thread
Task 53 is running on thread
Task 54 is running on thread
Task 55 is running on thread
Task 56 is running on thread
Task 57 is running on thread
Task 58 is running on thread
Task 59 is running on thread
Task 60 is running on thread
Task 61 is running on thread
Task 62 is running on thread
Task 63 is running on thread
Task 64 is running on thread
Task 65 is running on thread
Task 66 is running on thread
Task 67 is running on thread
Task 68 is running on thread
Task 69 is running on thread
Task 70 is running on thread
Task 71 is running on thread
Task 72 is running on thread
Task 73 is running on thread
Task 74 is running on thread
Task 75 is running on thread
Task 76 is running on thread
Task 77 is running on thread
Task 78 is running on thread
Task 79 is running on thread
Task 80 is running on thread
Task 81 is running on thread
Task 82 is running on thread
Task 83 is running on thread
Task 84 is running on thread
Task 85 is running on thread
Task 86 is running on thread
Task 87 is running on thread
Task 88 is running on thread
Task 89 is running on thread
Task 90 is running on thread
Task 91 is running on thread
Task 92 is running on thread
Task 93 is running on thread
Task 94 is running on thread
Task 95 is running on thread
Task 96 is running on thread
Task 97 is running on thread
Task 98 is running on thread
Task 99 is running on thread
All tasks completed.
2024/7/13 16:24:20
2024/7/13 16:24:31
0:00:00:10.9477683

多核循环代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    internal class Program
    {
        static void Main(string[] args)
        {
            DateTime start;
            DateTime end;
            TimeSpan expend;
            int n = 100; 

            start = DateTime.Now;
            Parallel.For(0, n, i =>
            {
                Console.WriteLine($"Task {i} is running on thread {Task.CurrentId}");
                DoSomeWork(i);
            });

            end = DateTime.Now;
            expend= end-start;
            Console.WriteLine("All tasks completed.");
            Console.WriteLine(start.ToString("G"));
            Console.WriteLine(end.ToString("G"));
            Console.WriteLine( expend.ToString("G"));
            Console.Read();
        }

        static void DoSomeWork(int i)
        {
            Task.Delay(100).Wait();
        }
    }
}

多核循环输出:

Task 0 is running on thread 1
Task 6 is running on thread 2
Task 12 is running on thread 3
Task 18 is running on thread 6
Task 24 is running on thread 8
Task 30 is running on thread 9
Task 36 is running on thread 12
Task 42 is running on thread 14
Task 48 is running on thread 16
Task 54 is running on thread 18
Task 60 is running on thread 20
Task 66 is running on thread 21
Task 72 is running on thread 24
Task 78 is running on thread 26
Task 84 is running on thread 27
Task 90 is running on thread 30
Task 96 is running on thread 32
Task 91 is running on thread 37
Task 67 is running on thread 42
Task 73 is running on thread 40
Task 61 is running on thread 44
Task 97 is running on thread 36
Task 85 is running on thread 38
Task 55 is running on thread 43
Task 13 is running on thread 56
Task 37 is running on thread 49
Task 31 is running on thread 50
Task 49 is running on thread 48
Task 43 is running on thread 53
Task 79 is running on thread 39
Task 19 is running on thread 54
Task 1 is running on thread 34
Task 2 is running on thread 1
Task 25 is running on thread 51
Task 7 is running on thread 55
Task 8 is running on thread 55
Task 9 is running on thread 57
Task 26 is running on thread 51
Task 32 is running on thread 50
Task 98 is running on thread 36
Task 50 is running on thread 48
Task 44 is running on thread 53
Task 86 is running on thread 38
Task 62 is running on thread 44
Task 56 is running on thread 43
Task 74 is running on thread 40
Task 20 is running on thread 54
Task 92 is running on thread 37
Task 68 is running on thread 42
Task 38 is running on thread 49
Task 80 is running on thread 39
Task 14 is running on thread 56
Task 4 is running on thread 71
Task 3 is running on thread 1
Task 10 is running on thread 1
Task 15 is running on thread 92
Task 93 is running on thread 96
Task 45 is running on thread 102
Task 21 is running on thread 97
Task 57 is running on thread 99
Task 51 is running on thread 103
Task 69 is running on thread 95
Task 27 is running on thread 106
Task 75 is running on thread 98
Task 39 is running on thread 93
Task 33 is running on thread 104
Task 5 is running on thread 71
Task 81 is running on thread 94
Task 63 is running on thread 100
Task 99 is running on thread 105
Task 87 is running on thread 101
Task 16 is running on thread 92
Task 11 is running on thread 1
Task 64 is running on thread 100
Task 70 is running on thread 95
Task 76 is running on thread 98
Task 22 is running on thread 97
Task 46 is running on thread 102
Task 28 is running on thread 106
Task 40 is running on thread 93
Task 34 is running on thread 104
Task 82 is running on thread 94
Task 52 is running on thread 103
Task 94 is running on thread 96
Task 88 is running on thread 101
Task 58 is running on thread 99
Task 17 is running on thread 92
Task 89 is running on thread 101
Task 71 is running on thread 95
Task 47 is running on thread 102
Task 59 is running on thread 99
Task 41 is running on thread 93
Task 23 is running on thread 97
Task 65 is running on thread 100
Task 77 is running on thread 98
Task 95 is running on thread 96
Task 29 is running on thread 106
Task 35 is running on thread 104
Task 83 is running on thread 94
Task 53 is running on thread 103
All tasks completed.
2024/7/13 16:26:28
2024/7/13 16:26:32
0:00:00:03.3746510

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值