比Singlethreading慢多线程 [英] Multithreading slower than Singlethreading

查看:137
本文介绍了比Singlethreading慢多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码(控制台应用程序的Program.cs文件的全部内容)。
'COUNTUP'至'countUp4的单线程执行需要13秒,多线程执行21秒。



我有英特尔酷睿i5 -2400 @ 3.10 GHz的,8 GB内存,Windows 7的64位。那么,为什么辑阵线程执行比单线程1慢?



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

 使用系统; 
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用的System.Threading;

命名ConsoleApplication1
{
类节目
{
静态INT计数= 0;
静态INT计数器2 = 0;
静态INT计数器3 = 0;
静态INT counter4 = 0;

静态无效的主要(字串[] args)
{
Console.WriteLine(没有多线程:);
Console.WriteLine(开始+ DateTime.Now.ToString());

COUNTUP();
countUp2();
countUp3();
countUp4();

Console.WriteLine();
Console.WriteLine(多线程);
Console.WriteLine(开始+ DateTime.Now.ToString());

线程线程1 =新主题(新的ThreadStart(COUNTUP));
thread1.Start();
线程线程2 =新主题(新的ThreadStart(countUp2));
thread2.Start();
螺纹thread3 =新主题(新的ThreadStart(countUp3));
thread3.Start();
螺纹thread4 =新主题(新的ThreadStart(countUp4));
thread4.Start();

Console.Read();
}

静态无效COUNTUP()
{
为(双I = 0; I< 10亿;我++)
{
反++;
}

Console.WriteLine(counter.ToString());
Console.WriteLine(DateTime.Now.ToString());
}

静态无效countUp2()
{
为(双I = 0; I< 10亿;我++)
{
计数器2 ++;
}

Console.WriteLine(counter2.ToString());
Console.WriteLine(DateTime.Now.ToString());
}

静态无效countUp3()
{
为(双I = 0; I< 10亿;我++)
{
计数器3 ++;
}

Console.WriteLine(counter3.ToString());
Console.WriteLine(DateTime.Now.ToString());
}

静态无效countUp4()
{
为(双I = 0; I< 10亿;我++)
{
counter4 ++;
}

Console.WriteLine(counter4.ToString());
Console.WriteLine(DateTime.Now.ToString());
}
}
}


解决方案

下面是一个原因,你可能看不到未来:假共享因为这些4整数。都坐在并排在内存



文章(阅读它 - 它的光辉)展示了如何在更新时是通过在内存中并排值可以最终导致阻塞因为他们都坐在同一高速缓存行。这是很低级的封锁,你不能从你的.NET代码禁用。可以,但是强制的数据被进一步间隔开,这样你保证,或至少增加的可能性,即每一个值将是在一个不同的高速缓存行。



本文采用阵列 - 但它只是可能的话在这里影响到你。



要跟进下面的建议,您也许能证明/反驳这种通过不断变化的代码 - 所以,稍微:

 类节目
{
类CounterHolder {
私人诠释[ ] fakeInts =新INT [1024];
公共int值= 0;
}
静态CounterHolder计数器1 =新CounterHolder();
静态CounterHolder计数器2 =新CounterHolder();
静态CounterHolder计数器3 =新CounterHolder();
静态CounterHolder counter4 =新CounterHolder();



然后修改你的线程函数来处理公共领域每个柜台持有人。



我做了这些阵列比他们需要,希望它会更好地证明它确实大得多:)


I have the following Code (complete content of 'Program.cs' of console application). The single threaded execution of 'countUp' till 'countUp4' takes 13 sec., the multi threaded execution 21 sec..

I have a Intel Core i5-2400 @ 3.10 GHz, 8 GB Ram, Windows 7 64 Bit. So why is the mutli threaded execution slower than the single threaded one?

Is multithreading just useful for not blocking the main routine of simple c# applications? When does multithreading give me an advantage in execution speed?

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());
        }
    }
}

解决方案

Here's a cause that you might not see coming: false sharing because those 4 ints all sit side by side in memory.

The article (read it all - it's brilliant) shows how values that are side by side in memory can end up causing blocking when updated because they all sit on the same cache line. This is very low-level blocking that you can't disable from your .Net code. You can, however force the data to be spaced further apart so that you guarantee, or at least increase the likelihood, that each value will be on a different cache line.

The article uses arrays - but it's just possible it's affecting you here.

To follow up the suggestion below, you might be able to prove/disprove this by changing your code ever-so-slightly:

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(); 

And then modify your thread functions to manipulate the public field Value on each of the counter holders.

I've made those arrays really much bigger than they need to be in the hope that it'll prove it better :)

这篇关于比Singlethreading慢多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆