为什么我的多线程比单线程慢? [英] Why is my multi-threading slower than my single threading?

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

问题描述

我知道有几个人问了类似的问题,但是我找不到任何让我理解为什么它变慢的回答.

因此,我制作了一个小控制台程序,以了解Visual Studio 2013中的线程对象.我的CPU是Intel Core i7,可以使用多个线程.

我的代码:

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

namespace ConsoleApplication1
{
    class Program
    {

        static TimeSpan MTTime;
        static TimeSpan STTime;

        static void Main(string[] args)
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();


            Console.WriteLine(Environment.NewLine + "---------------Multi Process-------------" + Environment.NewLine);

            Thread th1 = new Thread(new ParameterizedThreadStart(Process));
            Thread th2 = new Thread(new ParameterizedThreadStart(Process));
            Thread th3 = new Thread(new ParameterizedThreadStart(Process));
            Thread th4 = new Thread(new ParameterizedThreadStart(Process));

            th1.Start("A");
            th2.Start("B");
            th3.Start("C");
            th4.Start("D");

            th1.Join();
            th2.Join();
            th3.Join();
            th4.Join();

            stopwatch.Stop();
            MTTime = stopwatch.Elapsed ;

            Console.WriteLine(Environment.NewLine + "---------------Single Process-------------" + Environment.NewLine);


            stopwatch.Reset();
            stopwatch.Start();

            Process("A");
            Process("B");
            Process("C");
            Process("D");

            stopwatch.Stop();
            STTime = stopwatch.Elapsed;

            Console.Write(Environment.NewLine + Environment.NewLine + "Multi  : "+ MTTime + Environment.NewLine + "Single : " + STTime);


            Console.ReadKey();
        }

        static void Process(object procName)
        {
            for (int i = 0; i < 100; i++)
            {
                Console.Write(procName);
            }
        }
    }
}

结果图片:

我们可以清楚地看到,多次踩踏过程是完全随机的,而一个踩踏过程只是一个接一个地进行,但是我认为这不会影响速度.

起初,我认为我的线程仅比运行该程序所需的进程大,但是在更改为更大的进程后,单步执行仍然在很大程度上是最快的.那么,我是否想念多线程中的一个概念?还是比较慢?

解决方案

从官方控制台文档

使用这些流的

I/O操作是同步的,这意味着 多个线程可以从流中读取或写入流.这 表示通常是异步的方法,例如 TextReader.ReadLineAsync,如果对象同步执行 代表控制台流

这意味着控制台类处理线程同步,因此,如果线程A和线程B试图写入控制台,则控制台将处理它们,并且只有一个时间可以写.其背后的 处理逻辑 是需要较长时间的原因


更新
我建议您看看 Parallel.ForEach

I know a couple of people asked a question similar to this, but I can’t find any response that would make me understand why it's slower.

So, I made a little console program for my own understanding of the threading object in Visual Studio 2013. My CPU is an Intel Core i7 that supply can use multiple threading.

My code:

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

namespace ConsoleApplication1
{
    class Program
    {

        static TimeSpan MTTime;
        static TimeSpan STTime;

        static void Main(string[] args)
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();


            Console.WriteLine(Environment.NewLine + "---------------Multi Process-------------" + Environment.NewLine);

            Thread th1 = new Thread(new ParameterizedThreadStart(Process));
            Thread th2 = new Thread(new ParameterizedThreadStart(Process));
            Thread th3 = new Thread(new ParameterizedThreadStart(Process));
            Thread th4 = new Thread(new ParameterizedThreadStart(Process));

            th1.Start("A");
            th2.Start("B");
            th3.Start("C");
            th4.Start("D");

            th1.Join();
            th2.Join();
            th3.Join();
            th4.Join();

            stopwatch.Stop();
            MTTime = stopwatch.Elapsed ;

            Console.WriteLine(Environment.NewLine + "---------------Single Process-------------" + Environment.NewLine);


            stopwatch.Reset();
            stopwatch.Start();

            Process("A");
            Process("B");
            Process("C");
            Process("D");

            stopwatch.Stop();
            STTime = stopwatch.Elapsed;

            Console.Write(Environment.NewLine + Environment.NewLine + "Multi  : "+ MTTime + Environment.NewLine + "Single : " + STTime);


            Console.ReadKey();
        }

        static void Process(object procName)
        {
            for (int i = 0; i < 100; i++)
            {
                Console.Write(procName);
            }
        }
    }
}

Result image:

We can clearly see that the multi-treading process is total random and the single one just do all presses on after the other, but I don't think this have an impact on the speed.

At first, I thought my thread was just bigger than the process needed for running the program, but after changing for a bigger process the single treading still was still the fastest in a big way. So, do i miss a concept in multi-threading? Or it normal that is slower?

解决方案

From the official Console documentation

I/O operations that use these streams are synchronized, which means that multiple threads can read from, or write to, the streams. This means that methods that are ordinarily asynchronous, such as TextReader.ReadLineAsync, execute synchronously if the object represents a console stream

This means that the console class handles the thread synchronization so if thread A and thread B are trying to write to the console the console will handle them and only one for time will be able to write. The handling logic behind it is the reason why it takes longer


UPDATE
I suggest you to have a look at Parallel.ForEach

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

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