13,152,454 members (37,048 online)
Rate this:
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.
```
/*****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

SHOUTING removed - OriginalGriff[/edit]
Posted 22-Sep-11 7:44am
Updated 24-Sep-11 20:08pm
v7
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.

Rate this:

## 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.
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

Ratika Agarwal 24-Sep-11 11:09am

Top Experts
Last 24hrsThis month
 Richard MacCutchan 273 OriginalGriff 190 Graeme_Grant 145 Dave Kreskowiak 109 Richard Deeming 100
 OriginalGriff 6,781 Graeme_Grant 5,246 ppolymorphe 2,179 Jochen Arndt 1,994 CPallini 1,950

Web02 | 2.8.170924.2 | Last Updated 3 Jan 2012