【问题标题】:Why Non-Threaded is working faster than Multi-Threaded?为什么非线程比多线程工作得更快?
【发布时间】:2013-12-09 06:12:45
【问题描述】:

我在比较线程和非线程应用程序,为什么非线程应用程序更快?

// makes thread
private void MakeThreads(int n)
{
    for (int i = 0; i < n; i++)
    {
        Thread thread = new Thread(PerformOperation);
        _threads.Add(thread);
        thread.Start();
    }
}

// any operation
private void PerformOperation()
{
    int j = 0;
    for (int i = 0; i < 999999; i++)
    {
        j++;
    }
}

private void Threaded_Click(object sender, EventArgs e)
{
    const int outer = 1000;
    const int inner = 2;

    Stopwatch timer = Stopwatch.StartNew();
    for (int i = 0; i < outer; i++)
    {
        MakeThreads(inner);
    }
    timer.Stop();
    TimeSpan timespan = timer.Elapsed;

    MessageBox.Show("Time Taken by " + (outer * inner) + " Operations: " +
        String.Format("{0:00}:{1:00}:{2:00}", timespan.Minutes, timespan.Seconds, timespan.Milliseconds / 10),
        "Result",
        MessageBoxButtons.OK,
        MessageBoxIcon.Information);
}

private void NonThreaded_Click(object sender, EventArgs e)
{
    const int outer = 1000;
    const int inner = 2;

    Stopwatch timer = Stopwatch.StartNew();
    for (int i = 0; i < inner * outer; i++)
    {
        PerformOperation();
    }
    timer.Stop();
    TimeSpan timespan = timer.Elapsed;

    MessageBox.Show("Time Taken by " + (outer * inner) + " Operations: " +
        String.Format("{0:00}:{1:00}:{2:00}", timespan.Minutes, timespan.Seconds, timespan.Milliseconds / 10),
        "Result",
        MessageBoxButtons.OK,
        MessageBoxIcon.Information);
}

线程时间:00:19:43 非线程时间:00:08:72

为什么我的线程花费了太多时间?我是不是搞错了?

【问题讨论】:

    标签: c# multithreading


    【解决方案1】:

    因为您创建了太多线程。创建线程需要更多时间,而且您的代码是错误的,您不是在等待线程完成。

    private void Threaded_Click(object sender, EventArgs e)
    {
        const int outer = 1000;
        const int inner = 2;
    
        Stopwatch timer = Stopwatch.StartNew();
        Parallel.For(0, inner*outer, i=> {
    
            PerformOperation();
        });
        timer.Stop();
        TimeSpan timespan = timer.Elapsed;
    
        MessageBox.Show("Time Taken by " + (outer * inner) + " Operations: " +
            String.Format("{0:00}:{1:00}:{2:00}", timespan.Minutes, timespan.Seconds, timespan.Milliseconds / 10),
            "Result",
            MessageBoxButtons.OK,
            MessageBoxIcon.Information);
    }
    

    【讨论】:

    • 如果我等待一个线程完成,那么多线程的优势是什么?
    • 您不等待单线程,而是等待所有线程完成。先试试这个,然后告诉我你是否注意到它的好处。
    • 现在这是我真正想要的东西。谢谢,这行得通。
    • 另外,到 999999 的循环是几个 9 的不足,需要合理的工作量。
    【解决方案2】:

    方法“Threaded_Click()”将在后台线程(非 UI 线程)上执行代码。而方法“NonThreaded_Click()”将在 UI 线程(前台线程)上执行代码。 这就是它比其他更早执行的原因。

    您可以通过更改“MakeThreads()”使它们在相同的时间间隔上执行,如下所示

    private void MakeThreads(int n)
            {
                for (int i = 0; i < n; i++)
                {                
                    var task = Task.Factory.StartNew(PerformOperation);             
                    task.Wait();
                }
            }
    

    但这会冻结 UI 线程。

    【讨论】:

    • 这也很有帮助,谢谢。它已将时间减至最少。但 Akash Kava 的答案是最好的
    猜你喜欢
    • 1970-01-01
    • 2013-11-22
    • 1970-01-01
    • 1970-01-01
    • 2021-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-21
    相关资源
    最近更新 更多