【发布时间】:2015-03-12 02:19:41
【问题描述】:
我只是想尝试多线程应用程序并检查使用多线程时的性能。
但我不知道是我做错了还是我误解了!我是编程的业余爱好者,甚至是初学者。
因为在普通模式下(不使用线程),完成该过程所需的时间更短!例如:
使用线程: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();
}
}
}
【问题讨论】:
-
你究竟想达到什么目的?
-
我只是想检查一下使用线程性能和不使用线程性能执行该测试功能(myfunction)有什么区别!但我想知道是因为单线程更快!
-
运行更多代码(如
Invoke)应该需要更多时间...有什么特别让你吃惊的? -
@AlexeiLevenkov 我很惊讶,因为我认为使用多线程会比单线程快得多!但结果反之亦然!
-
调用
.Invoke(...)将委托推送到 UI 线程。myfunction()中的其余代码非常快 - 所以大部分工作都发生在 UI 线程上。如此有效地,您在 UI 线程上运行相同数量的工作,同时使用您的代码的线程和非线程版本。启动新线程和调用.Invoke的开销需要更长的时间。仅当您在非 UI 线程上执行计算成本很高的事情时,线程化才有用。
标签: c# multithreading performance