c# – 多线程比单线程慢

c# – 多线程比单线程慢,第1张

概述我有以下代码(控制台应用程序的“Program.cs”的完整内容). ‘countUp’直到’countUp4’的单线程执行需要13秒,多线程执行21秒.. 我有Intel Core i5-2400 @ 3.10 GHz,8 GB Ram,Windows 7 64位.那么为什么mutli线程执行比单线程执行慢? 多线程只是用于阻止简单的c#应用程序的主程序吗?什么时候多线程在执行速度上给我一个优势 我有以下代码(控制台应用程序的“Program.cs”的完整内容).
‘countUp’直到’countUp4’的单线程执行需要13秒,多线程执行21秒..

我有Intel Core i5-2400 @ 3.10 GHz,8 GB Ram,Windows 7 64位.那么为什么mutli线程执行比单线程执行慢?

多线程只是用于阻止简单的c#应用程序的主程序吗?什么时候多线程在执行速度上给我一个优势?

using System;using System.Collections.Generic;using System.linq;using System.Text;using System.Threading;namespace ConsoleApplication1{    class Program    {        static int counter = 0;        static int counter2 = 0;        static int counter3 = 0;        static int counter4 = 0;        static voID Main(string[] args)        {            Console.Writeline("Without multithreading:");            Console.Writeline("Start:" + DateTime.Now.ToString());            countUp();            countUp2();            countUp3();            countUp4();            Console.Writeline("");            Console.Writeline("With multithreading:");            Console.Writeline("Start:" + DateTime.Now.ToString());            Thread thread1 = new Thread(new ThreadStart(countUp));            thread1.Start();            Thread thread2 = new Thread(new ThreadStart(countUp2));            thread2.Start();            Thread thread3 = new Thread(new ThreadStart(countUp3));            thread3.Start();            Thread thread4 = new Thread(new ThreadStart(countUp4));            thread4.Start();            Console.Read();        }        static voID countUp()        {            for (double i = 0; i < 1000000000; i++)            {                counter++;            }            Console.Writeline(counter.ToString());            Console.Writeline(DateTime.Now.ToString());        }        static voID countUp2()        {            for (double i = 0; i < 1000000000; i++)            {                counter2++;            }            Console.Writeline(counter2.ToString());            Console.Writeline(DateTime.Now.ToString());        }        static voID countUp3()        {            for (double i = 0; i < 1000000000; i++)            {                counter3++;            }            Console.Writeline(counter3.ToString());            Console.Writeline(DateTime.Now.ToString());        }        static voID countUp4()        {            for (double i = 0; i < 1000000000; i++)            {                counter4++;            }            Console.Writeline(counter4.ToString());            Console.Writeline(DateTime.Now.ToString());        }    }}
解决方法 这是一个你可能看不到来的原因: false sharing因为这4个内存都并排在内存中.

更新 – 前几年的MSDN mags现在只能作为.chm文件使用 – 所以你必须抓住‘October 2008’ edition of the MSDN Mag from here,下载后,您必须记住在windows资源管理器中的文件属性对话框中右键单击并解压文件其他 *** 作系统可用!)打开之前.您正在寻找Stephen Toub,Igor Ostrovsky和Huseyin Yildiz的“.Net Matters”列

文章(阅读全文 – 它是辉煌的)显示了内存中并排的值可能会最终导致更新时阻塞,因为它们都位于同一个高速缓存行上.这是非常低级别的阻止,你不能从你的.Net代码禁用.但是,您可以强制将数据间隔开,以便您保证或至少增加每个值在不同高速缓存行上的可能性.

该文章使用数组 – 但它只是可能在这里影响你.

为了跟进下面的建议,您可以通过稍微更改代码来证明/反驳这一点:

class Program {     class CounterHolder {       private int[] fakeInts = new int[1024];       public int Value = 0;    }    static CounterHolder counter1 = new CounterHolder();     static CounterHolder counter2 = new CounterHolder();     static CounterHolder counter3 = new CounterHolder();     static CounterHolder counter4 = new CounterHolder();

然后修改你的线程函数来 *** 纵每个计数器持有者的公共字段值.

我使这些数组真的比他们需要的更大,希望它会证明它更好:)

总结

以上是内存溢出为你收集整理的c# – 多线程比单线程慢全部内容,希望文章能够帮你解决c# – 多线程比单线程慢所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1236026.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存