I have build an application in c# with .NET 4.0 to compare two images using parallel library. I have a PC with two cores, but when i run the app in one core i runs faster than when i run it in two.
this is the method that compares images.
private unsafe Bitmap krahas(Bitmap nje, Bitmap dy)
{
Bitmap a = new Bitmap(nje);
Bitmap b = new Bitmap(dy);
int gjersia = Math.Min(a.Width, b.Width);
int lartesia = Math.Min(a.Height, b.Height);
Bitmap rez = new Bitmap(gjersia, lartesia);
int numri_i_procesorve = Numri_i_berthamave.Value;
Rectangle per_a = new Rectangle(0, 0, a.Width, a.Height);
Rectangle per_b = new Rectangle(0, 0, b.Width, b.Height);
Rectangle per_rez = new Rectangle(0, 0, gjersia, lartesia);
BitmapData t_per_a = a.LockBits(per_a, ImageLockMode.ReadWrite, a.PixelFormat);
BitmapData t_per_b = b.LockBits(per_b, ImageLockMode.ReadWrite, b.PixelFormat);
BitmapData t_per_rez = rez.LockBits(per_rez, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
byte* adresa_per_a = (byte*)t_per_a.Scan0;
byte* adresa_per_b = (byte*)t_per_b.Scan0;
byte* rezult = (byte*)t_per_rez.Scan0;
Color cl = Color.Red;
byte[] ngj = new byte[4];
ngj[3] = cl.A;
ngj[2] = cl.R;
ngj[1] = cl.G;
ngj[0] = cl.B;
int rowpadding = t_per_a.Stride - (a.Width * 4);
ParallelOptions po = new ParallelOptions();
po.MaxDegreeOfParallelism = numri_i_procesorve;
koha_e_kaluar.Reset();
koha_e_kaluar.Start();
object lok = new object();
Parallel.For(0, lartesia, po, i =>
{
lock (lok)
{
for (int j = 0; j < gjersia; j++)
{
int same = 0;
byte[] tmp = new byte[4];
for (int x = 0; x < 4; x++)
{
tmp[x] = adresa_per_b[0];
if (adresa_per_a[0] == adresa_per_b[0])
{
same++;
}
adresa_per_a++;
adresa_per_b++;
}
for (int x = 0; x < 4; x++)
{
rezult[0] = (same != 4) ? ngj[x] : tmp[x];
rezult++;
}
}
if (rowpadding > 0)
{
adresa_per_a += rowpadding;
adresa_per_b += rowpadding;
rezult += rowpadding;
}
}
});
koha_e_kaluar.Stop();
a.UnlockBits(t_per_a);
b.UnlockBits(t_per_b);
rez.UnlockBits(t_per_rez);
return rez;
}
I don't now where is the problem. Can anyone help me! Please.