Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
C#
/*****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]SHOUTING removed - OriginalGriff[/edit]
Posted
Updated 24-Sep-11 20:08pm
v7
Comments
OriginalGriff 22-Sep-11 14:11pm    
DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalisation if you want to be taken seriously.
Ratika Agarwal 22-Sep-11 14:14pm    
ok. thanks.

1 solution

I'm not surprised: there is a both an extra loop in the parallel version:
C#
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.
 
Share this answer
 
Comments
Ratika Agarwal 22-Sep-11 15:14pm    
Now check the following code.
There is change only in parallel version loop. But then also the time concept is opposite.



int[,] a = { { 3, 1, 2 }, { 5, 4, 6 }, { 2, 5, 4 }, { 5, 9, 1 } };
var sw = Stopwatch.StartNew();
int[] t = new int[4];

/**********************Parallel code START of Finding largest in 2-d MATRIX***********
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];
}
}
Console.WriteLine("Largest no.t["+i+"] is = " + t[i]);
}
);
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*******************/

/***************Sequentially Finding largest in 2-d MATRIX***/
int i;
for (i = 0; i < 4; i++)
{
t[i] = a[i, 0];
for (int j = 1; j < 3; j++)
{
if (t[i] < a[i, j])
{
t[i] = a[i, j];
}
}
Console.WriteLine("Largest no.t["+i+"] is = " + t[i]);
}
int x = t[0];
for (int j = 1; j < t.Length; j++)
{
if (x < t[j])
{
x = t[j];
}
}
Console.WriteLine("Largest element = " + x);


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


Console.ReadLine();
Ratika Agarwal 24-Sep-11 11:09am    
anybody please answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900