请对此提供帮助.为什么并行执行时间比此处给出的顺序代码要多. [英] Please help somebody regarding this..Why is parallel execution time is coming more than sequential code given here..

查看:86
本文介绍了请对此提供帮助.为什么并行执行时间比此处给出的顺序代码要多.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

并行执行时间比顺序代码更多.(代码已在双核处理器上执行.)
为什么会这样?

首先运行此代码,然后注释并行代码&取消注释顺序代码,然后再次运行它&检查两个运行时间.

Parallel execution time is more than sequential code.(Code has been executed on dual core processor.)
Why is it so??

First run this, then comment the parallel code & uncomment sequential code, then again run it & check both the run times.

/*****NOTICE:- Please do not run both, sequential & parallel codes together**/

     /*******Code for sequentially Finding largest in 2-d MATRIX***
            var sw = Stopwatch.StartNew();
            int[,] a = { { 3, 1, 2 }, { 5, 4, 6 }, { 2, 3, 4 }, { 5, 9, 1 } };
            int t = 0;
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    if (t < a[i, j])
                    {
                        t = a[i, j];
                    }
                }
            }
            Console.WriteLine("Largest element = " + t);
      ****END of sequential code********/

            /*******Parallel START of Finding largest in 2-d MATRIX******/
                    int[,] a = { { 3, 1, 2 }, { 5, 4, 6 }, { 2, 5, 4 } ,{5,9,1}};

                    var sw = Stopwatch.StartNew();
                    int[] t = new int[4];
                    Parallel.For(0, 4, (int i) =>
                        {
                            t[i] = a[i, 0];
                            for (int j = 1; j < 3; j++)
                            {
                                if (t[i] < a[i,j])
                                {
                                    t[i] = a[i,j];
                                }
                            }
                        }
                        );
                    int x = t[0];
                    for (int j = 1; j < t.Length; j++)
                    {
                        if (x < t[j])
                        {
                            x = t[j];
                        }
                    }
                    Console.WriteLine("Largest no. = " + x);

        /********END of Finding largest in 2-d MATRIX**********/




            TimeSpan s = sw.Elapsed;                    // For calculation
            Console.WriteLine("Run time = " + s);     // of timespan

            Console.ReadLine();



[edit]删除了喊话-OriginalGriff [/edit]



[edit]SHOUTING removed - OriginalGriff[/edit]

推荐答案

我并不感到惊讶:并行版本中同时存在一个额外的循环:
I''m not surprised: there is a both an extra loop in the parallel version:
for (int j = 1; j < t.Length; j++)
{
    if (x < t[j])
    {
        x = t[j];
    }
}

并且并行部分只有四个迭代,并且顶部还有额外的线程启动开销.

根据您拥有的内核数量以及系统的其他活动,您可能(也可能不会)获得任何实际的并行处理-如果没有其他可用的内核来承担负载,则它可能最终在同一内核上以顺序运行.

And the parallel portion is only four iterations and there is the extra thread startup overhead on top.

Depending on the number of cores you have, and the other activity of the system, you may (or may not) get any real parallel processing - it could end up as sequential on the same core if no other cores are available to take the load.


这篇关于请对此提供帮助.为什么并行执行时间比此处给出的顺序代码要多.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆