Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0 Parallel
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]SHOUTING removed - OriginalGriff[/edit]
Posted 22-Sep-11 8:44am
Edited 24-Sep-11 21:08pm
v7
Comments
OriginalGriff at 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 at 22-Sep-11 14:14pm
   
ok. thanks.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Ratika Agarwal at 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 at 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)



Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 3 Jan 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100