在C#中尝试MultiThread

时间:2015-03-12 02:19:42

标签: c# multithreading performance

我只是想尝试MultiThreading应用程序,并检查使用多线程时的性能。

但我不知道我做错了还是我误解了!我是编程方面的业余爱好者,甚至是初学者。

因为在正常模式下(不使用线程),完成该过程所需的时间更短!例如:

使用线程:02.8500253但不使用线程:02.5455425

有时差异较大!

我的问题是:我做错了还是我误解了多线程等等我想知道什么是错的?为什么没有线程在这里更快!?

这里的整个代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;
namespace WindowsFormsApplication57
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    Stopwatch sw = new Stopwatch();

    private void button1_Click(object sender, EventArgs e)
    {
    //    System.Windows.Forms.Timer mytimer = new System.Windows.Forms.Timer();
   //     mytimer.Interval = 1;
        sw.Reset();

        sw.Start();

        Thread thread1 = new Thread(new ThreadStart(myfunction));
        thread1.Start();
        Thread thread2 = new Thread(new ThreadStart(myfunction));
        thread2.Start();

       // sw.Stop();
      //  for (int i = 0; i < mylist.Count; i++) 
     //   {
    //        listBox1.Items.Add(mylist[i]);
    //    }
        //listBox1.Items.Add
        //label1.Text = string.Format("Elapsed Time Using MultiThread= {0}", sw.Elapsed);

   //     mytimer.Enabled = true;
    }

    List<string> mylist = new List<string>();

    void myfunction()
    {    

        for (int i = 0; i < 10000; i++) 
        {
            mylist.Add(i.ToString());

     //       listBox1.Items.Add(i);
            if (listBox1.InvokeRequired)
            {
                listBox1.Invoke(new MethodInvoker(delegate { listBox1.Items.Add(i); }));
            }
            else { listBox1.Items.Add(i); }
        }
        if (mylist.Count == 20000)
        {
            sw.Stop();
            if (label1.InvokeRequired)
            {
                label1.Invoke(new MethodInvoker(delegate { label1.Text = string.Format("Elapsed Time Using MultiThread= {0}", sw.Elapsed); }));
            }
            else
            {
                label1.Text = string.Format("Elapsed Time Using MultiThread= {0}", sw.Elapsed);
            }
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        //Stopwatch sw = new Stopwatch();
        sw.Reset();
        sw.Start();

        myfunction();
        myfunction();

        //sw.Stop();
    //    for (int i = 0; i < mylist.Count; i++)
    //    {
   //         listBox1.Items.Add(mylist[i]);
   //     }
        label1.Text = string.Format("Elapsed Time WITHOUT MultiThread= {0}", sw.Elapsed);
    }

    private void button3_Click(object sender, EventArgs e)
    {
        mylist.Clear();
        listBox1.Items.Clear();
    }

}
}

1 个答案:

答案 0 :(得分:4)

多线程不一定比单个线程运行得更快,记住创建/调度线程需要很多CPU周期。例如,如果您在单核CPU中运行代码,多线程实际上会使速度变慢 - 尽管单核CPU在现代PC上并不常见。

mylist中添加20000个字符串只需几毫秒,在listBox1.Invoke上花费了99%的CPU时间。

在您的代码中,您调用listBox1.Invoke来编组对UI线程的调用,因此来自两个线程的代码listBox1.Items.Add(i);最终在同一UI线程上运行 ,通过这种方式,在单个线程上运行没有明显的改进(如果有的话)。

你可以尝试这个listBox1.Items.AddRange(mylist),这只会被调用一次,而不是20000次。

相关问题