前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Parallel.ForEach与foreach相比的优势怎么体现[通俗易懂]

Parallel.ForEach与foreach相比的优势怎么体现[通俗易懂]

作者头像
全栈程序员站长
发布2022-09-09 11:30:38
5860
发布2022-09-09 11:30:38
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

为什么80%的码农都做不了架构师?>>>

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public class People
    {
        private int id;

        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        private int age;

        public int Age
        {
            get { return age; }
            set { age = value; }
        }
        private Byte sex;

        public Byte Sex
        {
            get { return sex; }
            set { sex = value; }
        }
        private string school;

        public string School
        {
            get { return school; }
            set { school = value; }
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            List<People> pd = new List<People>();
            for(int i = 0; i < 72000; i++)
            {
                People people = new People();
                people.Id = i;
                people.Sex = 1;
                if(i<36000){
                    people.Age = 23;
                    people.Name= "MJ" + i;
                    people.School="NC";
                }else
                {
                    people.Name= "KD" + i;
                    people.Age = 24;
                    people.School="UCLA";
                }
                pd.Add(people);
            }

            System.DateTime currentTime0 = System.DateTime.Now;
            foreach (People p in pd)
            {
                p.Id += 1;
                p.Name = "aaa";
                p.Age = 40;
                p.Sex = 1;
                Console.WriteLine(p.Id + ", " + p.Name + ", " + p.School);
            }
            System.DateTime currentTime1 = System.DateTime.Now;
            Parallel.ForEach(pd, p =>
            {
                p.Id += 1;
                p.Name = "bbb";
                p.Age = 35;
                p.Sex =0;
                Console.WriteLine(p.Id + ", " + p.Name + ", " + p.School);
            });
            System.DateTime currentTime2 = System.DateTime.Now;

            Console.WriteLine(currentTime0.ToString() + " " + currentTime0.Millisecond.ToString());
            Console.WriteLine(currentTime1.ToString() + " " + currentTime1.Millisecond.ToString());
            Console.WriteLine(currentTime2.ToString() + " " + currentTime2.Millisecond.ToString());
            Console.ReadKey();
        }
    }
}

输出过程中,明显看到第二个循环比第一个快了很多很多,但是看最后的时间差却失望了:

2014/08/29 15:37:50 401

2014/08/29 15:37:59 246

2014/08/29 15:38:08 185

转载于:https://my.oschina.net/mj23/blog/308043

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/161809.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档