我是靠谱客的博主 友好红牛,最近开发中收集的这篇文章主要介绍mysql多线程写入为什么比单线程写入慢,为什么我的多线程比单线程慢?,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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:

uwfsy.png

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

最后

以上就是友好红牛为你收集整理的mysql多线程写入为什么比单线程写入慢,为什么我的多线程比单线程慢?的全部内容,希望文章能够帮你解决mysql多线程写入为什么比单线程写入慢,为什么我的多线程比单线程慢?所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(31)

评论列表共有 0 条评论

立即
投稿
返回
顶部