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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值